
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// Z88DK CODE SIZE REDUCTION FOR LONG LONG
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

replace restart {
	ld	de,#%1 + %3
	ld	hl,#%2
	add	hl,sp
	ld	bc,#0x0008
	ldir
} by {
	ld	de,#%1+%3
	ld	hl,#%2
	add	hl,sp
	ld	bc,#0x0008
	ldir
	; peephole z88dk-code-size-reduction-1a
}

replace restart {
	ld	de,#%1
	ld	hl,#%2
	add	hl,sp
	ld	bc,#0x0008
	ldir
} by {
	ld	de,#%1
	ld	hl,#%2 + 2
	call	____sdcc_ll_copy_src_hlsp_dst_de
	; peephole z88dk-code-size-reduction-1b
}

replace restart {
	ld	de,#%1 + %3
	ld	hl,#%2
	add	hl,sp
	ex	de,hl
	ld	bc,#0x0008
	ldir
} by {
	ld	de,#%1+%3
	ld	hl,#%2
	add	hl,sp
	ex	de,hl
	ld	bc,#0x0008
	ldir
	; peephole z88dk-code-size-reduction-2a
}

replace restart {
	ld	de,#%1
	ld	hl,#%2
	add	hl,sp
	ex	de,hl
	ld	bc,#0x0008
	ldir
} by {
	ld	de,#%1
	ld	hl,#%2 + 2
	call	____sdcc_ll_copy_src_de_dst_hlsp
	; peephole z88dk-code-size-reduction-2b
}

replace restart {
	ld	de,#%1
	ld	hl,#%2 + 2
	call	____sdcc_ll_copy_src_hlsp_dst_de
	ld	de,#%1
	ld	hl,#%2 + 2
	call	____sdcc_ll_copy_src_de_dst_hlsp
} by {
	ld	de,#%1
	ld	hl,#%2 + 2
	call	____sdcc_ll_copy_src_hlsp_dst_de
	ex	de,hl
	; peephole z88dk-code-size-reduction-3
}

replace restart {
	ld	a,%9 (ix)
	add	a, %1 (ix)
	ld	%1 (ix),a
	ld	a,%10 (ix)
	adc	a, %2 (ix)
	ld	%2 (ix),a
	ld	a,%11 (ix)
	adc	a, %3 (ix)
	ld	%3 (ix),a
	ld	a,%12 (ix)
	adc	a, %4 (ix)
	ld	%4 (ix),a
	ld	a,%13 (ix)
	adc	a, %5 (ix)
	ld	%5 (ix),a
	ld	a,%14 (ix)
	adc	a, %6 (ix)
	ld	%6 (ix),a
	ld	a,%15 (ix)
	adc	a, %7 (ix)
	ld	%7 (ix),a
	ld	a,%16 (ix)
	adc	a, %8 (ix)
	ld	%8 (ix),a
} by {
	push	de
	push	hl
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_add_deix_hlix
	pop	hl
	pop	de
	; peephole z88dk-code-size-reduction-4a
}

replace restart {
	push	%4
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_add_deix_hlix
	pop	%4
} by {
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_add_deix_hlix
	; peephole z88dk-code-size-reduction-4b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_add_deix_hlix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_add_deix_hlix
	pop	%5
	; peephole z88dk-code-size-reduction-4c
} if notUsed(%4)

replace restart {
	ld	a,%1 (ix)
	sub	a, %9 (ix)
	ld	%1 (ix),a
	ld	a,%2 (ix)
	sbc	a, %10 (ix)
	ld	%2 (ix),a
	ld	a,%3 (ix)
	sbc	a, %11 (ix)
	ld	%3 (ix),a
	ld	a,%4 (ix)
	sbc	a, %12 (ix)
	ld	%4 (ix),a
	ld	a,%5 (ix)
	sbc	a, %13 (ix)
	ld	%5 (ix),a
	ld	a,%6 (ix)
	sbc	a, %14 (ix)
	ld	%6 (ix),a
	ld	a,%7 (ix)
	sbc	a, %15 (ix)
	ld	%7 (ix),a
	ld	a,%8 (ix)
	sbc	a, %16 (ix)
	ld	%8 (ix),a
} by {
	push	de
	push	hl
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_sub_deix_hlix
	pop	hl
	pop	de
	; peephole z88dk-code-size-reduction-5a
}

replace restart {
	push	%4
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_sub_deix_hlix
	pop	%4
} by {
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_sub_deix_hlix
	; peephole z88dk-code-size-reduction-5b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_sub_deix_hlix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	de,#%1
	ld	hl,#%9
	call	____sdcc_ll_sub_deix_hlix
	pop	%5
	; peephole z88dk-code-size-reduction-5c
} if notUsed(%4)

replace restart {
	ld	a,%1 (ix)
	push	af
	inc	sp
	ld	a,%2 (ix)
	push	af
	inc	sp
	ld	a,%3 (ix)
	push	af
	inc	sp
	ld	a,%4 (ix)
	push	af
	inc	sp
	ld	a,%5 (ix)
	push	af
	inc	sp
	ld	a,%6 (ix)
	push	af
	inc	sp
	ld	a,%7 (ix)
	push	af
	inc	sp
	ld	a,%8 (ix)
	push	af
	inc	sp
} by {
	ld	hl,#%1
	call	____sdcc_ll_push_hlix
	; peephole z88dk-code-size-reduction-6a
} if notUsed('de'), notUsed('hl')

replace restart {
	ld	e,%1 (ix)
	ld	d,%2 (ix)
	ld	hl,#%3
	add	hl,sp
	ld	bc,#0x0008
	ldir
} by {
	ld	de,#%1
	ld	hl,#%3 + 2
	call	____sdcc_ll_copy_src_hlsp_dst_deixm
	; peephole z88dk-code-size-reduction-7
}

replace restart {
	ld	e,%1 (ix)
	ld	d,%2 (ix)
	ld	hl,#%3
	add	hl,sp
	ex	de,hl
	ld	bc,#0x0008
	ldir
} by {
	ld	de,#%1
	ld	hl,#%3 + 2
	call	____sdcc_ll_copy_src_deixm_dst_hlsp
	; peephole z88dk-code-size-reduction-8
}

replace restart {
	ld	de,#%1
	ld	hl,#%3 + 2
	call	____sdcc_ll_copy_src_hlsp_dst_deixm
	ld	de,#%1
	ld	hl,#%3 + 2
	call	____sdcc_ll_copy_src_deixm_dst_hlsp
} by {
	ld	de,#%1
	ld	hl,#%3 + 2
	call	____sdcc_ll_copy_src_hlsp_dst_deixm
	ex	de,hl
	; peephole z88dk-code-size-reduction-9
}	

replace restart {
	inc	b
	jp	%2
%3:
	sra	%4 (ix)
	rr	%5 (ix)
	rr	%6 (ix)
	rr	%7 (ix)
	rr	%8 (ix)
	rr	%9 (ix)
	rr	%10 (ix)
	rr	%11 (ix)
%2:
	djnz	%3
} by {
	push	bc
	push	de
	push	hl
	ld	a,b
	ld	hl,%11
	call	____sdcc_ll_asr_hlix_a
	pop	hl
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-size-reduction-10a1
} 
//if notUsed('a')

replace restart {
	ld	b,%1
%3:
	sra	%4 (ix)
	rr	%5 (ix)
	rr	%6 (ix)
	rr	%7 (ix)
	rr	%8 (ix)
	rr	%9 (ix)
	rr	%10 (ix)
	rr	%11 (ix)
	djnz	%3
} by {
	push	bc
	push	de
	push	hl
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_asr_hlix_a
	pop	hl
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-size-reduction-10a2
} 
//if notUsed('a')

replace restart {
	push	%4
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_asr_hlix_a
	pop	%4
} by {
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_asr_hlix_a
	; peephole z88dk-code-size-reduction-10b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_asr_hlix_a
	pop	%5
	pop	%4
} by {
	push	%5
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_asr_hlix_a
	pop	%5
	; peephole z88dk-code-size-reduction-10c
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	push	%6
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_asr_hlix_a
	pop	%6
	pop	%5
	pop	%4
} by {
	push	%5
	push	%6
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_asr_hlix_a
	pop	%6
	pop	%5
	; peephole z88dk-code-size-reduction-10d
} if notUsed(%4)

replace restart {
	inc	b
	jp	%2
%3:
	srl	%4 (ix)
	rr	%5 (ix)
	rr	%6 (ix)
	rr	%7 (ix)
	rr	%8 (ix)
	rr	%9 (ix)
	rr	%10 (ix)
	rr	%11 (ix)
%2:
	djnz	%3
} by {
	push	bc
	push	de
	push	hl
	ld	a,b
	ld	hl,%11
	call	____sdcc_ll_lsr_hlix_a
	pop	hl
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-size-reduction-11a1
} 
//if notUsed('a')

replace restart {
	ld	b,%1
%3:
	srl	%4 (ix)
	rr	%5 (ix)
	rr	%6 (ix)
	rr	%7 (ix)
	rr	%8 (ix)
	rr	%9 (ix)
	rr	%10 (ix)
	rr	%11 (ix)
	djnz	%3
} by {
	push	bc
	push	de
	push	hl
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsr_hlix_a
	pop	hl
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-size-reduction-11a2
} 
//if notUsed('a')

replace restart {
	push	%4
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsr_hlix_a
	pop	%4
} by {
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsr_hlix_a
	; peephole z88dk-code-size-reduction-11b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsr_hlix_a
	pop	%5
	pop	%4
} by {
	push	%5
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsr_hlix_a
	pop	%5
	; peephole z88dk-code-size-reduction-11c
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	push	%6
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsr_hlix_a
	pop	%6
	pop	%5
	pop	%4
} by {
	push	%5
	push	%6
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsr_hlix_a
	pop	%6
	pop	%5
	; peephole z88dk-code-size-reduction-11d
} if notUsed(%4)

replace restart {
	inc	b
	jp	%2
%3:
	sla	%4 (ix)
	rl	%5 (ix)
	rl	%6 (ix)
	rl	%7 (ix)
	rl	%8 (ix)
	rl	%9 (ix)
	rl	%10 (ix)
	rl	%11 (ix)
%2:
	djnz	%3
} by {
	push	bc
	push	de
	push	hl
	ld	a,b
	ld	hl,%4
	call	____sdcc_ll_lsl_hlix_a
	pop	hl
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-size-reduction-12a1
} 
//if notUsed('a')

replace restart {
	ld	b,%1
%3:
	sla	%4 (ix)
	rl	%5 (ix)
	rl	%6 (ix)
	rl	%7 (ix)
	rl	%8 (ix)
	rl	%9 (ix)
	rl	%10 (ix)
	rl	%11 (ix)
	djnz	%3
} by {
	push	bc
	push	de
	push	hl
	ld	a,%1
	ld	hl,%4
	call	____sdcc_ll_lsl_hlix_a
	pop	hl
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-size-reduction-12a2
} 
//if notUsed('a')

replace restart {
	push	%4
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsl_hlix_a
	pop	%4
} by {
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsl_hlix_a
	; peephole z88dk-code-size-reduction-12b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsl_hlix_a
	pop	%5
	pop	%4
} by {
	push	%5
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsl_hlix_a
	pop	%5
	; peephole z88dk-code-size-reduction-12c
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	push	%6
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsl_hlix_a
	pop	%6
	pop	%5
	pop	%4
} by {
	push	%5
	push	%6
	ld	a,%1
	ld	hl,%11
	call	____sdcc_ll_lsl_hlix_a
	pop	%6
	pop	%5
	; peephole z88dk-code-size-reduction-12d
} if notUsed(%4)

replace restart {
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
} by {
	call	____sdcc_ll_push_mhl
	; peephole z88dk-code-size-reduction-13a
} if notUsed('de'), notUsed('hl')

replace restart {
	ld	(hl),a
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
	dec	hl
	ld	a,(hl)
	push	af
	inc	sp
} by {
	ld	(hl),a
	call	____sdcc_ll_push_mhl
	; peephole z88dk-code-size-reduction-13b
} if notUsed('de'), notUsed('hl')

replace restart {
	ld	a,%1 (ix)
	ld	hl,#%10
	ld	(hl),a
	ld	a,%2 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%3 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%4 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%5 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%6 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%7 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%8 (ix)
	inc	hl
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	de,%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hl
	pop	de
	pop	bc
	ld	hl,#%10 + 7
	; peephole z88dk-code-size-reduction-14a
}
// notUsed('a')

replace restart {
	ld	a,%1 (ix)
	ld	hl,#%10 + %11
	ld	(hl),a
	ld	a,%2 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%3 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%4 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%5 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%6 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%7 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%8 (ix)
	inc	hl
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	de,%1
	ld	hl,#%10+%11
	call	____sdcc_ll_copy_src_deix_dst_hl
	pop	de
	pop	bc
	ld	hl,#%10+%11 + 7
	; peephole z88dk-code-size-reduction-14b
}
// notUsed('a')

replace restart {
	push	%4
	ld	de,%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hl
	pop	%4
} by {
	ld	de,%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hl
	; peephole z88dk-code-size-reduction-14c
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	de,%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hl
	pop	%5
	pop	%4
} by {
	push	%5
	ld	de,%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hl
	pop	%5
	; peephole z88dk-code-size-reduction-14d
} if notUsed(%4)

replace restart {
	ld	hl,#%10
	ld	a,(hl)
	ld	hl,#%20
	add	a, (hl)
	ld	%1 (ix),a
	ld	hl,#%10 + %11
	ld	a,(hl)
	ld	hl,#%20 + %21
	adc	a, (hl)
	ld	%2 (ix),a
	ld	hl,#%10 + %12
	ld	a,(hl)
	ld	hl,#%20 + %22
	adc	a, (hl)
	ld	%3 (ix),a
	ld	hl,#%10 + %13
	ld	a,(hl)
	ld	hl,#%20 + %23
	adc	a, (hl)
	ld	%4 (ix),a
	ld	hl,#%10 + %14
	ld	a,(hl)
	ld	hl,#%20 + %24
	adc	a, (hl)
	ld	%5 (ix),a
	ld	hl,#%10 + %15
	ld	a,(hl)
	ld	hl,#%20 + %25
	adc	a, (hl)
	ld	%6 (ix),a
	ld	hl,#%10 + %16
	ld	a,(hl)
	ld	hl,#%20 + %26
	adc	a, (hl)
	ld	%7 (ix),a
	ld	hl,#%10 + %17
	ld	a,(hl)
	ld	hl,#%20 + %27
	adc	a, (hl)
	ld	%8 (ix),a
} by {
	push	bc
	push	de
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_add_deix_bc_hl
	pop	de
	pop	bc
	; peephole z88dk-code-size-reduction-15a
}

replace restart {
	push	%4
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_add_deix_bc_hl
	pop	%4
} by {
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_add_deix_bc_hl
	; peephole z88dk-code-size-reduction-15b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_add_deix_bc_hl
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_add_deix_bc_hl
	pop	%5
	; peephole z88dk-code-size-reduction-15c
} if notUsed(%4)

replace restart {
	ld	hl,#%10
	ld	a,(hl)
	ld	hl,#%20
	sub	a, (hl)
	ld	%1 (ix),a
	ld	hl,#%10 + %11
	ld	a,(hl)
	ld	hl,#%20 + %21
	sbc	a, (hl)
	ld	%2 (ix),a
	ld	hl,#%10 + %12
	ld	a,(hl)
	ld	hl,#%20 + %22
	sbc	a, (hl)
	ld	%3 (ix),a
	ld	hl,#%10 + %13
	ld	a,(hl)
	ld	hl,#%20 + %23
	sbc	a, (hl)
	ld	%4 (ix),a
	ld	hl,#%10 + %14
	ld	a,(hl)
	ld	hl,#%20 + %24
	sbc	a, (hl)
	ld	%5 (ix),a
	ld	hl,#%10 + %15
	ld	a,(hl)
	ld	hl,#%20 + %25
	sbc	a, (hl)
	ld	%6 (ix),a
	ld	hl,#%10 + %16
	ld	a,(hl)
	ld	hl,#%20 + %26
	sbc	a, (hl)
	ld	%7 (ix),a
	ld	hl,#%10 + %17
	ld	a,(hl)
	ld	hl,#%20 + %27
	sbc	a, (hl)
	ld	%8 (ix),a
} by {
	push	bc
	push	de
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_sub_deix_bc_hl
	pop	de
	pop	bc
	; peephole z88dk-code-size-reduction-16a
}

replace restart {
	push	%4
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_sub_deix_bc_hl
	pop	%4
} by {
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_sub_deix_bc_hl
	; peephole z88dk-code-size-reduction-16b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_sub_deix_bc_hl
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%10
	ld	de,#%1
	ld	hl,#%20
	call	____sdcc_ll_sub_deix_bc_hl
	pop	%5
	; peephole z88dk-code-size-reduction-16c
} if notUsed(%4)

replace restart {
	ld	hl,#%1
	add	hl,sp
	ex	de,hl
	ld	hl,#%2
	add	hl,sp
	ld	bc,#8
	ldir
} by {
	ld	de,#%2 + 2
	ld	hl,#%1 + 2
	call	____sdcc_ll_copy_src_desp_dst_hlsp
	; peephole z88dk-code-size-reduction-17
}

replace restart {
	ld	hl,#%10
	ld	a,(hl)
	ld	%1 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%2 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%3 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%4 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%5 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%6 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%7 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%8 (ix),a
} by {
	push	bc
	push	de
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_copy_src_de_dst_hlix
	pop	de
	pop	bc
	ld	hl,#%10 + 7
	; peephole z88dk-code-size-reduction-18a1
}
// notUsed('a')

replace restart {
	ld	hl,#%10 + %11
	ld	a,(hl)
	ld	%1 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%2 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%3 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%4 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%5 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%6 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%7 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%8 (ix),a
} by {
	push	bc
	push	de
	ld	de,#%10+%11
	ld	hl,#%1
	call	____sdcc_ll_copy_src_de_dst_hlix
	pop	de
	pop	bc
	ld	hl,#%10+%11 + 7
	; peephole z88dk-code-size-reduction-18a2
}
// notUsed('a')

replace restart {
	push	%4
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_copy_src_de_dst_hlix
	pop	%4
} by {
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_copy_src_de_dst_hlix
	; peephole z88dk-code-size-reduction-18b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_copy_src_de_dst_hlix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_copy_src_de_dst_hlix
	pop	%5
	; peephole z88dk-code-size-reduction-18c
} if notUsed(%4)

replace restart {
	ld	hl,#%1
	ld	a,(hl)
	ld	hl,#%2
	add	a, (hl)
	ld	hl,#%3
	ld	(hl),a
	ld	hl,#%1 + %11
	ld	a,(hl)
	ld	hl,#%2 + %21
	adc	a, (hl)
	ld	hl,#%3 + %31
	ld	(hl),a
	ld	hl,#%1 + %12
	ld	a,(hl)
	ld	hl,#%2 + %22
	adc	a, (hl)
	ld	hl,#%3 + %32
	ld	(hl),a
	ld	hl,#%1 + %13
	ld	a,(hl)
	ld	hl,#%2 + %23
	adc	a, (hl)
	ld	hl,#%3 + %33
	ld	(hl),a
	ld	hl,#%1 + %14
	ld	a,(hl)
	ld	hl,#%2 + %24
	adc	a, (hl)
	ld	hl,#%3 + %34
	ld	(hl),a
	ld	hl,#%1 + %15
	ld	a,(hl)
	ld	hl,#%2 + %25
	adc	a, (hl)
	ld	hl,#%3 + %35
	ld	(hl),a
	ld	hl,#%1 + %16
	ld	a,(hl)
	ld	hl,#%2 + %26
	adc	a, (hl)
	ld	hl,#%3 + %36
	ld	(hl),a
	ld	hl,#%1 + %17
	ld	a,(hl)
	ld	hl,#%2 + %27
	adc	a, (hl)
	ld	hl,#%3 + %37
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_add_de_bc_hl
	pop	de
	pop	bc
	ld	hl,#%3 + %37
	; peephole z88dk-code-reduction-19a
}

replace restart {
	push	%4
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_add_de_bc_hl
	pop	%4
} by {
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_add_de_bc_hl
	; peephole z88dk-code-reduction-19b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_add_de_bc_hl
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_add_de_bc_hl
	pop	%5
	; peephole z88dk-code-reduction-19c
} if notUsed(%4)

replace restart {
	ld	hl,#%1
	ld	a,(hl)
	ld	hl,#%2
	sub	a, (hl)
	ld	hl,#%3
	ld	(hl),a
	ld	hl,#%1 + %11
	ld	a,(hl)
	ld	hl,#%2 + %21
	sbc	a, (hl)
	ld	hl,#%3 + %31
	ld	(hl),a
	ld	hl,#%1 + %12
	ld	a,(hl)
	ld	hl,#%2 + %22
	sbc	a, (hl)
	ld	hl,#%3 + %32
	ld	(hl),a
	ld	hl,#%1 + %13
	ld	a,(hl)
	ld	hl,#%2 + %23
	sbc	a, (hl)
	ld	hl,#%3 + %33
	ld	(hl),a
	ld	hl,#%1 + %14
	ld	a,(hl)
	ld	hl,#%2 + %24
	sbc	a, (hl)
	ld	hl,#%3 + %34
	ld	(hl),a
	ld	hl,#%1 + %15
	ld	a,(hl)
	ld	hl,#%2 + %25
	sbc	a, (hl)
	ld	hl,#%3 + %35
	ld	(hl),a
	ld	hl,#%1 + %16
	ld	a,(hl)
	ld	hl,#%2 + %26
	sbc	a, (hl)
	ld	hl,#%3 + %36
	ld	(hl),a
	ld	hl,#%1 + %17
	ld	a,(hl)
	ld	hl,#%2 + %27
	sbc	a, (hl)
	ld	hl,#%3 + %37
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_sub_de_bc_hl
	pop	de
	pop	bc
	ld	hl,#%3 + %37
	; peephole z88dk-code-reduction-20a
}

replace restart {
	push	%4
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_sub_de_bc_hl
	pop	%4
} by {
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_sub_de_bc_hl
	; peephole z88dk-code-reduction-20b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_sub_de_bc_hl
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%1
	ld	de,#%3
	ld	hl,#%2
	call	____sdcc_ll_sub_de_bc_hl
	pop	%5
	; peephole z88dk-code-reduction-20c
} if notUsed(%4)

replace restart {
	ld	hl,#%1
	ld	a,(hl)
	ld	hl,#%2
	ld	(hl),a
	ld	hl,#%1 + %11
	ld	a,(hl)
	ld	hl,#%2 + %21
	ld	(hl),a
	ld	hl,#%1 + %12
	ld	a,(hl)
	ld	hl,#%2 + %22
	ld	(hl),a
	ld	hl,#%1 + %13
	ld	a,(hl)
	ld	hl,#%2 + %23
	ld	(hl),a
	ld	hl,#%1 + %14
	ld	a,(hl)
	ld	hl,#%2 + %24
	ld	(hl),a
	ld	hl,#%1 + %15
	ld	a,(hl)
	ld	hl,#%2 + %25
	ld	(hl),a
	ld	hl,#%1 + %16
	ld	a,(hl)
	ld	hl,#%2 + %26
	ld	(hl),a
	ld	hl,#%1 + %17
	ld	a,(hl)
	ld	hl,#%2 + %27
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	de,#%2
	ld	hl,#%1
	call	____sdcc_ll_copy_src_hl_dst_de
	pop	de
	pop	bc
	ld	hl,#%2 + %27
	; peephole z88dk-code-reduction-21a
}
// notUsed('a')

replace {
	push	%4
	ld	de,#%2
	ld	hl,#%1
	call	____sdcc_ll_copy_src_hl_dst_de
	pop	%4
} by {
	ld	de,#%2
	ld	hl,#%1
	call	____sdcc_ll_copy_src_hl_dst_de
	; peephole z88dk-code-reduction-21b
} if notUsed(%4)

replace {
	push	%4
	push	%5
	ld	de,#%2
	ld	hl,#%1
	call	____sdcc_ll_copy_src_hl_dst_de
	pop	%5
	pop	%4
} by {
	push	%5
	ld	de,#%2
	ld	hl,#%1
	call	____sdcc_ll_copy_src_hl_dst_de
	pop	%5
	; peephole z88dk-code-reduction-21c
} if notUsed(%4)

replace restart {
	inc	b
	jp	%1
%2:
	ld	hl,#%3 + %4
	sra	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
%1:
	djnz	%2
} by {
	push	bc
	push	de
	ld	a,b
	ld	bc,#%3+%4 - 7
	call	____sdcc_ll_asr_mbc_a
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-reduction-22a1
} 
//if notUsed('a')

replace restart {
	ld	b,%1
%2:
	ld	hl,#%3 + %4
	sra	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	djnz	%2
} by {
	push	bc
	push	de
	ld	a,%1
	ld	bc,#%3+%4 - 7
	call	____sdcc_ll_asr_mbc_a
	pop	de
	pop	bc
	ld	b,0
	ld	hl,#%3 + %4-7
	; peephole z88dk-code-reduction-22a2
} 
//if notUsed('a')

replace restart {
	push	%4
	ld	a,%1
	ld	bc,#%3 - 7
	call	____sdcc_ll_asr_mbc_a
	pop	%4
} by {
	ld	a,%1
	ld	bc,#%3 - 7
	call	____sdcc_ll_asr_mbc_a
	; peephole z88dk-code-reduction-22b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	a,%1
	ld	bc,#%3 - 7
	call	____sdcc_ll_asr_mbc_a
	pop	%5
	pop	%4
} by {
	push	%5
	ld	a,%1
	ld	bc,#%3 - 7
	call	____sdcc_ll_asr_mbc_a
	pop	%5
	; peephole z88dk-code-reduction-22c
} if notUsed(%4)

replace restart {
	inc	b
	jp	%1
%2:
	ld	hl,#%3
	sla	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
%1:
	djnz	%2
} by {
	push	bc
	push	de
	ld	a,b
	ld	bc,#%3
	call	____sdcc_ll_lsl_mbc_a
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-reduction-23a1
} 
//if notUsed('a')

replace restart {
	ld	b,%1
%2:
	ld	hl,#%3
	sla	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	inc	hl
	rl	(hl)
	djnz	%2
} by {
	push	bc
	push	de
	ld	a,%1
	ld	bc,#%3
	call	____sdcc_ll_lsl_mbc_a
	pop	de
	pop	bc
	ld	b,0
	ld	hl,#%3 + 7
	; peephole z88dk-code-reduction-23a2
} 
//if notUsed('a')

replace restart {
	push	%4
	ld	a,%1
	ld	bc,#%3
	call	____sdcc_ll_lsl_mbc_a
	pop	%4
} by {
	ld	a,%1
	ld	bc,#%3
	call	____sdcc_ll_lsl_mbc_a
	; peephole z88dk-code-reduction-23b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	a,%1
	ld	bc,#%3
	call	____sdcc_ll_lsl_mbc_a
	pop	%5
	pop	%4
} by {
	push	%5
	ld	a,%1
	ld	bc,#%3
	call	____sdcc_ll_lsl_mbc_a
	pop	%5
	; peephole z88dk-code-reduction-23c
} if notUsed(%4)

replace restart {
	inc	b
	jp	%1
%2:
	ld	hl,#%3 + %4
	srl	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
%1:
	djnz	%2
} by {
	push	bc
	push	de
	ld	a,b
	ld	bc,#%3+%4 - 7
	call	____sdcc_ll_lsr_mbc_a
	pop	de
	pop	bc
	ld	b,0
	; peephole z88dk-code-reduction-24a1
} 
//if notUsed('a')

replace restart {
	ld	b,%1
%2:
	ld	hl,#%3 + %4
	srl	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	dec	hl
	rr	(hl)
	djnz	%2
} by {
	push	bc
	push	de
	ld	a,%1
	ld	bc,#%3+%4 - 7
	call	____sdcc_ll_lsr_mbc_a
	pop	de
	pop	bc
	ld	b,0
	ld	hl,#%3 + %4-7
	; peephole z88dk-code-reduction-24a2
} 
//if notUsed('a')

replace restart {
	push	%4
	ld	a,%1
	ld	bc,#%3 - 7
	call	____sdcc_ll_lsr_mbc_a
	pop	%4
} by {
	ld	a,%1
	ld	bc,#%3 - 7
	call	____sdcc_ll_lsr_mbc_a
	; peephole z88dk-code-reduction-24b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	a,%1
	ld	bc,#%3 - 7
	call	____sdcc_ll_lsr_mbc_a
	pop	%5
	pop	%4
} by {
	push	%5
	ld	a,%1
	ld	bc,#%3 - 7
	call	____sdcc_ll_lsr_mbc_a
	pop	%5
	; peephole z88dk-code-reduction-24c
} if notUsed(%4)

replace restart {
	ld	a,%10 (ix)
	add	a, %20 (ix)
	ld	%1 (ix),a
	ld	a,%11 (ix)
	adc	a, %21 (ix)
	ld	%2 (ix),a
	ld	a,%12 (ix)
	adc	a, %22 (ix)
	ld	%3 (ix),a
	ld	a,%13 (ix)
	adc	a, %23 (ix)
	ld	%4 (ix),a
	ld	a,%14 (ix)
	adc	a, %24 (ix)
	ld	%5 (ix),a
	ld	a,%15 (ix)
	adc	a, %25 (ix)
	ld	%6 (ix),a
	ld	a,%16 (ix)
	adc	a, %26 (ix)
	ld	%7 (ix),a
	ld	a,%17 (ix)
	adc	a, %27 (ix)
	ld	%8 (ix),a
} by {
	push	bc
	push	de
	push	hl
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_add_hlix_deix_bcix
	pop	hl
	pop	de
	pop	bc
	; peephole z88dk-code-reduction-25a
}

replace restart {
	push	%4
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_add_hlix_deix_bcix
	pop	%4
} by {
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_add_hlix_deix_bcix
	; peephole z88dk-code-reduction-25b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_add_hlix_deix_bcix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_add_hlix_deix_bcix
	pop	%5
	; peephole z88dk-code-reduction-25c
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	push	%6
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_add_hlix_deix_bcix
	pop	%6
	pop	%5
	pop	%4
} by {
	push	%5
	push	%6
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_add_hlix_deix_bcix
	pop	%6
	pop	%5
	; peephole z88dk-code-reduction-25d
} if notUsed(%4)

replace restart {
	ld	a,%10 (ix)
	sub	a, %20 (ix)
	ld	%1 (ix),a
	ld	a,%11 (ix)
	sbc	a, %21 (ix)
	ld	%2 (ix),a
	ld	a,%12 (ix)
	sbc	a, %22 (ix)
	ld	%3 (ix),a
	ld	a,%13 (ix)
	sbc	a, %23 (ix)
	ld	%4 (ix),a
	ld	a,%14 (ix)
	sbc	a, %24 (ix)
	ld	%5 (ix),a
	ld	a,%15 (ix)
	sbc	a, %25 (ix)
	ld	%6 (ix),a
	ld	a,%16 (ix)
	sbc	a, %26 (ix)
	ld	%7 (ix),a
	ld	a,%17 (ix)
	sbc	a, %27 (ix)
	ld	%8 (ix),a
} by {
	push	bc
	push	de
	push	hl
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_sub_hlix_deix_bcix
	pop	hl
	pop	de
	pop	bc
	; peephole z88dk-code-reduction-26a
}

replace restart {
	push	%4
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_sub_hlix_deix_bcix
	pop	%4
} by {
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_sub_hlix_deix_bcix
	; peephole z88dk-code-reduction-26b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_sub_hlix_deix_bcix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_sub_hlix_deix_bcix
	pop	%5
	; peephole z88dk-code-reduction-26c
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	push	%6
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_sub_hlix_deix_bcix
	pop	%6
	pop	%5
	pop	%4
} by {
	push	%5
	push	%6
	ld	bc,#%20
	ld	de,#%10
	ld	hl,#%1
	call	____sdcc_ll_sub_hlix_deix_bcix
	pop	%6
	pop	%5
	; peephole z88dk-code-reduction-26d
} if notUsed(%4)

replace restart {
	push	af
	ld	a,%1 (ix)
	ld	%10 (ix),a
	ld	a,%2 (ix)
	ld	%11 (ix),a
	ld	a,%3 (ix)
	ld	%12 (ix),a
	ld	a,%4 (ix)
	ld	%13 (ix),a
	ld	a,%5 (ix)
	ld	%14 (ix),a
	ld	a,%6 (ix)
	ld	%15 (ix),a
	ld	a,%7 (ix)
	ld	%16 (ix),a
	ld	a,%8 (ix)
	ld	%17 (ix),a
	pop	af
} by {
	push	af
	push	bc
	push	de
	push	hl
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	pop	hl
	pop	de
	pop	bc
	pop	af
	; peephole z88dk-code-reduction-27a
}

replace restart {
	push	%4
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	pop	%4
} by {
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	; peephole z88dk-code-reduction-27b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	pop	%5
	; peephole z88dk-code-reduction-27c
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	push	%6
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	pop	%6
	pop	%5
	pop	%4
} by {
	push	%5
	push	%6
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	pop	%6
	pop	%5
	; peephole z88dk-code-reduction-27d
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	push	%6
	push	%7
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	pop	%7
	pop	%6
	pop	%5
	pop	%4
} by {
	push	%5
	push	%6
	push	%7
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_copy_src_deix_dst_hlix
	pop	%7
	pop	%6
	pop	%5
	; peephole z88dk-code-reduction-27e
} if notUsed(%4)

replace restart {
	ld	hl,#%1
	ld	a,(hl)
	add	a, %10 (ix)
	ld	%20 (ix),a
	inc	hl
	ld	a,(hl)
	adc	a, %11 (ix)
	ld	%21 (ix),a
	inc	hl
	ld	a,(hl)
	adc	a, %12 (ix)
	ld	%22 (ix),a
	inc	hl
	ld	a,(hl)
	adc	a, %13 (ix)
	ld	%23 (ix),a
	inc	hl
	ld	a,(hl)
	adc	a, %14 (ix)
	ld	%24 (ix),a
	inc	hl
	ld	a,(hl)
	adc	a, %15 (ix)
	ld	%25 (ix),a
	inc	hl
	ld	a,(hl)
	adc	a, %16 (ix)
	ld	%26 (ix),a
	inc	hl
	ld	a,(hl)
	adc	a, %17 (ix)
	ld	%27 (ix),a
} by {
	push	bc
	push	de
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_add_hlix_bc_deix
	pop	de
	pop	bc
	ld	hl,#%1 + 7
	; peephole z88dk-code-reduction-28a
}
// if notUsed('a')

replace restart {
	push	%4
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_add_hlix_bc_deix
	pop	%4
} by {
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_add_hlix_bc_deix
	; peephole z88dk-code-reduction-28b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_add_hlix_bc_deix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_add_hlix_bc_deix
	pop	%5
	; peephole z88dk-code-reduction-28c
} if notUsed(%4)

replace restart {
	ld	hl,#%1
	ld	a,(hl)
	sub	a, %10 (ix)
	ld	%20 (ix),a
	inc	hl
	ld	a,(hl)
	sbc	a, %11 (ix)
	ld	%21 (ix),a
	inc	hl
	ld	a,(hl)
	sbc	a, %12 (ix)
	ld	%22 (ix),a
	inc	hl
	ld	a,(hl)
	sbc	a, %13 (ix)
	ld	%23 (ix),a
	inc	hl
	ld	a,(hl)
	sbc	a, %14 (ix)
	ld	%24 (ix),a
	inc	hl
	ld	a,(hl)
	sbc	a, %15 (ix)
	ld	%25 (ix),a
	inc	hl
	ld	a,(hl)
	sbc	a, %16 (ix)
	ld	%26 (ix),a
	inc	hl
	ld	a,(hl)
	sbc	a, %17 (ix)
	ld	%27 (ix),a
} by {
	push	bc
	push	de
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_sub_hlix_bc_deix
	pop	de
	pop	bc
	ld	hl,#%1 + 7
	; peephole z88dk-code-reduction-29a
}

replace restart {
	push	%4
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_sub_hlix_bc_deix
	pop	%4
} by {
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_sub_hlix_bc_deix
	; peephole z88dk-code-reduction-29b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_sub_hlix_bc_deix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%1
	ld	de,#%10
	ld	hl,#%20
	call	____sdcc_ll_sub_hlix_bc_deix
	pop	%5
	; peephole z88dk-code-reduction-29c
} if notUsed(%4)

replace restart {
	ld	a,%1 (ix)
	ld	hl,#%20
	add	a, (hl)
	ld	%10 (ix),a
	ld	a,%2 (ix)
	inc	hl
	adc	a, (hl)
	ld	%11 (ix),a
	ld	a,%3 (ix)
	inc	hl
	adc	a, (hl)
	ld	%12 (ix),a
	ld	a,%4 (ix)
	inc	hl
	adc	a, (hl)
	ld	%13 (ix),a
	ld	a,%5 (ix)
	inc	hl
	adc	a, (hl)
	ld	%14 (ix),a
	ld	a,%6 (ix)
	inc	hl
	adc	a, (hl)
	ld	%15 (ix),a
	ld	a,%7 (ix)
	inc	hl
	adc	a, (hl)
	ld	%16 (ix),a
	ld	a,%8 (ix)
	inc	hl
	adc	a, (hl)
	ld	%17 (ix),a
} by {
	push	bc
	push	de
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_hlix_deix_bc
	pop	de
	pop	bc
	ld	hl,#%20 + 7
	; peephole z88dk-code-reduction-30a
}

replace restart {
	push	%4
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_hlix_deix_bc
	pop	%4
} by {
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_hlix_deix_bc
	; peephole z88dk-code-reduction-30b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_hlix_deix_bc
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_hlix_deix_bc
	pop	%5
	; peephole z88dk-code-reduction-30c
} if notUsed(%4)

replace restart {
	ld	a,%1 (ix)
	ld	hl,#%20
	sub	a, (hl)
	ld	%10 (ix),a
	ld	a,%2 (ix)
	inc	hl
	sbc	a, (hl)
	ld	%11 (ix),a
	ld	a,%3 (ix)
	inc	hl
	sbc	a, (hl)
	ld	%12 (ix),a
	ld	a,%4 (ix)
	inc	hl
	sbc	a, (hl)
	ld	%13 (ix),a
	ld	a,%5 (ix)
	inc	hl
	sbc	a, (hl)
	ld	%14 (ix),a
	ld	a,%6 (ix)
	inc	hl
	sbc	a, (hl)
	ld	%15 (ix),a
	ld	a,%7 (ix)
	inc	hl
	sbc	a, (hl)
	ld	%16 (ix),a
	ld	a,%8 (ix)
	inc	hl
	sbc	a, (hl)
	ld	%17 (ix),a
} by {
	push	bc
	push	de
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_hlix_deix_bc
	pop	de
	pop	bc
	ld	hl,#%20 + 7
	; peephole z88dk-code-reduction-31a
}

replace restart {
	push	%4
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_hlix_deix_bc
	pop	%4
} by {
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_hlix_deix_bc
	; peephole z88dk-code-reduction-31b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_hlix_deix_bc
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_hlix_deix_bc
	pop	%5
	; peephole z88dk-code-reduction-31c
} if notUsed(%4)

replace restart {
	ld	a,%10 (ix)
	add	a, %20 (ix)
	ld	hl,#%1
	ld	(hl),a
	ld	a,%11 (ix)
	adc	a, %21 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%12 (ix)
	adc	a, %22 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%13 (ix)
	adc	a, %23 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%14 (ix)
	adc	a, %24 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%15 (ix)
	adc	a, %25 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%16 (ix)
	adc	a, %26 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%17 (ix)
	adc	a, %27 (ix)
	inc	hl
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_de_hlix_bcix
	pop	de
	pop	bc
	ld	hl,#%1 + 7
	; peephole z88dk-code-reduction-32a
}

replace restart {
	push	%4
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_de_hlix_bcix
	pop	%4
} by {
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_de_hlix_bcix
	; peephole z88dk-code-reduction-32b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_de_hlix_bcix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_add_de_hlix_bcix
	pop	%5
	; peephole z88dk-code-reduction-32c
} if notUsed(%4)

replace restart {
	ld	a,%10 (ix)
	sub	a, %20 (ix)
	ld	hl,#%1
	ld	(hl),a
	ld	a,%11 (ix)
	sbc	a, %21 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%12 (ix)
	sbc	a, %22 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%13 (ix)
	sbc	a, %23 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%14 (ix)
	sbc	a, %24 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%15 (ix)
	sbc	a, %25 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%16 (ix)
	sbc	a, %26 (ix)
	inc	hl
	ld	(hl),a
	ld	a,%17 (ix)
	sbc	a, %27 (ix)
	inc	hl
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_de_hlix_bcix
	pop	de
	pop	bc
	ld	hl,#%1 + 7
	; peephole z88dk-code-reduction-33a
}

replace restart {
	push	%4
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_de_hlix_bcix
	pop	%4
} by {
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_de_hlix_bcix
	; peephole z88dk-code-reduction-33b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_de_hlix_bcix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%20
	ld	de,#%1
	ld	hl,#%10
	call	____sdcc_ll_sub_de_hlix_bcix
	pop	%5
	; peephole z88dk-code-reduction-33c
} if notUsed(%4)

replace restart {
	ld	hl,#%10
	ld	a,(hl)
	add	a, %1 (ix)
	ld	hl,#%20
	ld	(hl),a
	ld	hl,#%10 + %11
	ld	a,(hl)
	adc	a, %2 (ix)
	ld	hl,#%20 + %21
	ld	(hl),a
	ld	hl,#%10 + %12
	ld	a,(hl)
	adc	a, %3 (ix)
	ld	hl,#%20 + %22
	ld	(hl),a
	ld	hl,#%10 + %13
	ld	a,(hl)
	adc	a, %4 (ix)
	ld	hl,#%20 + %23
	ld	(hl),a
	ld	hl,#%10 + %14
	ld	a,(hl)
	adc	a, %5 (ix)
	ld	hl,#%20 + %24
	ld	(hl),a
	ld	hl,#%10 + %15
	ld	a,(hl)
	adc	a, %6 (ix)
	ld	hl,#%20 + %25
	ld	(hl),a
	ld	hl,#%10 + %16
	ld	a,(hl)
	adc	a, %7 (ix)
	ld	hl,#%20 + %26
	ld	(hl),a
	ld	hl,#%10 + %17
	ld	a,(hl)
	adc	a, %8 (ix)
	ld	hl,#%20 + %27
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_bc_hlix
	pop	de
	pop	bc
	ld	hl,#%20 + %27
	; peephole z88dk-code-reduction-34a
}

replace restart {
	push	%4
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_bc_hlix
	pop	%4
} by {
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_bc_hlix
	; peephole z88dk-code-reduction-34b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_bc_hlix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_bc_hlix
	pop	%5
	; peephole z88dk-code-reduction-34c
} if notUsed(%4)

replace restart {
	ld	hl,#%10
	ld	a,(hl)
	sub	a, %1 (ix)
	ld	hl,#%20
	ld	(hl),a
	ld	hl,#%10 + %11
	ld	a,(hl)
	sbc	a, %2 (ix)
	ld	hl,#%20 + %21
	ld	(hl),a
	ld	hl,#%10 + %12
	ld	a,(hl)
	sbc	a, %3 (ix)
	ld	hl,#%20 + %22
	ld	(hl),a
	ld	hl,#%10 + %13
	ld	a,(hl)
	sbc	a, %4 (ix)
	ld	hl,#%20 + %23
	ld	(hl),a
	ld	hl,#%10 + %14
	ld	a,(hl)
	sbc	a, %5 (ix)
	ld	hl,#%20 + %24
	ld	(hl),a
	ld	hl,#%10 + %15
	ld	a,(hl)
	sbc	a, %6 (ix)
	ld	hl,#%20 + %25
	ld	(hl),a
	ld	hl,#%10 + %16
	ld	a,(hl)
	sbc	a, %7 (ix)
	ld	hl,#%20 + %26
	ld	(hl),a
	ld	hl,#%10 + %17
	ld	a,(hl)
	sbc	a, %8 (ix)
	ld	hl,#%20 + %27
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_bc_hlix
	pop	de
	pop	bc
	ld	hl,#%20 + %27
	; peephole z88dk-code-reduction-35a
}

replace restart {
	push	%4
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_bc_hlix
	pop	%4
} by {
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_bc_hlix
	; peephole z88dk-code-reduction-35b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_bc_hlix
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_bc_hlix
	pop	%5
	; peephole z88dk-code-reduction-35c
} if notUsed(%4)

replace restart {
	ld	a,%1 (ix)
	ld	hl,#%10
	add	a, (hl)
	ld	hl,#%20
	ld	(hl),a
	ld	a,%2 (ix)
	ld	hl,#%10 + %11
	adc	a, (hl)
	ld	hl,#%20 + %21
	ld	(hl),a
	ld	a,%3 (ix)
	ld	hl,#%10 + %12
	adc	a, (hl)
	ld	hl,#%20 + %22
	ld	(hl),a
	ld	a,%4 (ix)
	ld	hl,#%10 + %13
	adc	a, (hl)
	ld	hl,#%20 + %23
	ld	(hl),a
	ld	a,%5 (ix)
	ld	hl,#%10 + %14
	adc	a, (hl)
	ld	hl,#%20 + %24
	ld	(hl),a
	ld	a,%6 (ix)
	ld	hl,#%10 + %15
	adc	a, (hl)
	ld	hl,#%20 + %25
	ld	(hl),a
	ld	a,%7 (ix)
	ld	hl,#%10 + %16
	adc	a, (hl)
	ld	hl,#%20 + %26
	ld	(hl),a
	ld	a,%8 (ix)
	ld	hl,#%10 + %17
	adc	a, (hl)
	ld	hl,#%20 + %27
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_hlix_bc
	pop	de
	pop	bc
	ld	hl,#%20 + %27
	; peephole z88dk-code-reduction-36a
}

replace restart {
	push	%4
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_hlix_bc
	pop	%4
} by {
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_hlix_bc
	; peephole z88dk-code-reduction-36b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_hlix_bc
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_add_de_hlix_bc
	pop	%5
	; peephole z88dk-code-reduction-36c
} if notUsed(%4)

replace restart {
	ld	a,%1 (ix)
	ld	hl,#%10
	sub	a, (hl)
	ld	hl,#%20
	ld	(hl),a
	ld	a,%2 (ix)
	ld	hl,#%10 + %11
	sbc	a, (hl)
	ld	hl,#%20 + %21
	ld	(hl),a
	ld	a,%3 (ix)
	ld	hl,#%10 + %12
	sbc	a, (hl)
	ld	hl,#%20 + %22
	ld	(hl),a
	ld	a,%4 (ix)
	ld	hl,#%10 + %13
	sbc	a, (hl)
	ld	hl,#%20 + %23
	ld	(hl),a
	ld	a,%5 (ix)
	ld	hl,#%10 + %14
	sbc	a, (hl)
	ld	hl,#%20 + %24
	ld	(hl),a
	ld	a,%6 (ix)
	ld	hl,#%10 + %15
	sbc	a, (hl)
	ld	hl,#%20 + %25
	ld	(hl),a
	ld	a,%7 (ix)
	ld	hl,#%10 + %16
	sbc	a, (hl)
	ld	hl,#%20 + %26
	ld	(hl),a
	ld	a,%8 (ix)
	ld	hl,#%10 + %17
	sbc	a, (hl)
	ld	hl,#%20 + %27
	ld	(hl),a
} by {
	push	bc
	push	de
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_hlix_bc
	pop	de
	pop	bc
	ld	hl,#%20 + %27
	; peephole z88dk-code-reduction-37a
}

replace restart {
	push	%4
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_hlix_bc
	pop	%4
} by {
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_hlix_bc
	; peephole z88dk-code-reduction-37b
} if notUsed(%4)

replace restart {
	push	%4
	push	%5
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_hlix_bc
	pop	%5
	pop	%4
} by {
	push	%5
	ld	bc,#%10
	ld	de,#%20
	ld	hl,#%1
	call	____sdcc_ll_sub_de_hlix_bc
	pop	%5
	; peephole z88dk-code-reduction-37c
} if notUsed(%4)

replace restart {
	call	____sdcc_lib_setmem_hl - 4-2-2-2-2-2-2-2-2-2-2-2-2-2-2-%1
} by {
	call	____sdcc_lib_setmem_hl - 28
	call	____sdcc_lib_setmem_hl - 4-%1
	; peephole z88dk-code-reduction-38a
}

replace restart {
	xor	a,a
	ld	(hl),a
	inc	hl
	ld	(hl),a
	inc	hl
} by {
	xor	a,a
	call	____sdcc_lib_setmem_hl - 4
	; peephole z88dk-code-reduction-38b
}

replace restart {
	call	____sdcc_lib_setmem_hl - %1
	ld	(hl),a
	inc	hl
} by {
	call	____sdcc_lib_setmem_hl - %1-2
	; peephole z88dk-code-reduction-38c
}

replace restart {
	call	____sdcc_lib_setmem_hl - %1
	ld	(hl),a
} by {
	call	____sdcc_lib_setmem_hl - %1-2
	; peephole z88dk-code-reduction-38d
} if notUsed('hl')

replace restart {
	xor	a,a
	call	____sdcc_lib_setmem_hl - %1
	ld	(hl),#0x00
	inc	hl
} by {
	xor	a,a
	call	____sdcc_lib_setmem_hl - %1-2
	; peephole z88dk-code-reduction-38e
}

replace restart {
	xor	a,a
	call	____sdcc_lib_setmem_hl - %1
	ld	(hl),#0x00
} by {
	xor	a,a
	call	____sdcc_lib_setmem_hl - %1-2
	; peephole z88dk-code-reduction-38f
} if notUsed('hl')


barrier


///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// Z88DK CODE SIZE REDUCTION FOR LONG AND FLOAT
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

replace restart {
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
	inc	hl
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
} by {
	call	____sdcc_load_debc_mhl
	; peephole z88dk-code-reduction-300a
}

replace restart {
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	inc	hl
	ld	a,(hl)
	inc	hl
	ld	h,(hl)
	ld	l,a
} by {
	call	____sdcc_load_hlde_mhl
	; peephole z88dk-code-reduction-301a
}

replace restart {
	ld	a,(hl)
	ld	%1 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%2 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%3 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%4 (ix),a
} by {
	ld	de,#%1
	call	____sdcc_4_copy_src_mhl_dst_deix
	; peephole z88dk-code-reduction-302a
} if notUsed('de')

replace restart {
	ld	a,(hl)
	ld	%1 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%2 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%3 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%4 (ix),a
} by {
	ld	bc,#%1
	call	____sdcc_4_copy_src_mhl_dst_bcix
	; peephole z88dk-code-reduction-303a
} if notUsed('bc')

replace restart {
	ld	a,(hl)
	ld	%1 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%2 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%3 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%4 (ix),a
} by {
	push	de
	ld	de,#%1
	call	____sdcc_4_copy_src_mhl_dst_deix
	pop	de
	; peephole z88dk-code-reduction-304a
}

replace restart {
	ld	(hl),c
	inc	hl
	ld	(hl),b
	inc	hl
	ld	(hl),e
	inc	hl
	ld	(hl),d
} by {
	call	____sdcc_store_debc_mhl
	; peephole z88dk-code-reduction-305a
}

replace restart {
	ld	a,(hl)
	ld	(bc),a
	inc	bc
	inc	hl
	ld	a,(hl)
	ld	(bc),a
	inc	bc
	inc	hl
	ld	a,(hl)
	ld	(bc),a
	inc	bc
	inc	hl
	ld	a,(hl)
	ld	(bc),a
} by {
	call	____sdcc_4_copy_src_mhl_dst_mbc
	; peephole z88dk-code-reduction-306a
}

replace restart {
	ld	hl,(#%1 + 2)
	push	hl
	ld	hl,(#%1)
	push	hl
} by {
	ld	hl,#%1
	call	____sdcc_4_push_mhl
	; peephole z88dk-code-reduction-307a
} if notUsed('hl'), notUsed('a')

replace restart {
	ld	c,l
	ld	b,h
	ld	hl,#%1
	call	____sdcc_store_debc_mhl
} by {
	ld	(#%1),hl
	ld	(#%1 + 2),de
	; peephole z88dk-code-reduction-308a
} if notUsed('hl'), notUsed('bc')

replace restart {
	ld	c,l
	ld	b,h
	ld	hl,#%1
	call	____sdcc_store_debc_mhl
} by {
	ld	(#%1),hl
	ld	(#%1 + 2),de
	ld	hl,#%1 + 3
	; peephole z88dk-code-reduction-308b
} if notUsed('bc')

replace restart {
	ldi
	ldi
	ldi
	ld	a,(hl)
	ld	(de),a
} by {
	call	____sdcc_4_ldi_save_bc
	; peephole z88dk-code-reduction-309a
} if notUsed('bc')

replace restart {
	ldi
	ldi
	ldi
	ld	a,(hl)
	ld	(de),a
} by {
	call	____sdcc_4_ldi_nosave_bc
	; peephole z88dk-code-reduction-309b
}

replace restart {
	ld	bc,#0x0004
	ldir
} by {
	call	____sdcc_4_ldi_save_bc
	; peephole z88dk-code-reduction-309c
} if notUsed('a'), notUsed('bc'), notUsed('de'), notUsed('hl')

replace restart {
	ld	bc,#4
	ldir
} by {
	call	____sdcc_4_ldi_save_bc
	; peephole z88dk-code-reduction-309d
} if notUsed('a'), notUsed('bc'), notUsed('de'), notUsed('hl')

replace restart {
	call	____sdcc_4_ldi_nosave_bc
} by {
	call	____sdcc_4_ldi_save_bc
	; peephole z88dk-code-reduction-309e
} if notUsed('bc')

replace restart {
	push	bc
	ex	de,hl
	ld	hl,#%1
	add	hl,sp
	call	____sdcc_4_ldi_save_bc
	pop	bc
} by {
	ex	de,hl
	ld	hl,#%1-2
	add	hl,sp
	call	____sdcc_4_ldi_save_bc
	; peephole z88dk-code-reduction-309f
}

replace restart {
	ld	l,%1 (ix)
	ld	h,%2 (ix)
	push	hl
	ld	l,%3 (ix)
	ld	h,%4 (ix)
	push	hl
} by {
	ld	hl,#%3
	call	____sdcc_4_push_hlix
	; peephole z88dk-code-reduction-310
} if notUsed('hl'), inSequence('1' %3 %4 %1 %2), notUsed('a')

replace restart {
	ld	%1 (ix),l
	ld	%2 (ix),h
	ld	%3 (ix),e
	ld	%4 (ix),d
} by {
	ld	bc,#%4
	call	____sdcc_store_dehl_bcix
	; peephole z88dk-code-reduction-311a
} if notUsed('bc'), inSequence('1' %1 %2 %3 %4)

replace restart {
	ld	%1 (ix),l
	ld	%2 (ix),h
	ld	%3 (ix),e
	ld	%4 (ix),d
} by {
	push	bc
	ld	bc,#%4
	call	____sdcc_store_dehl_bcix
	pop	bc
	; peephole z88dk-code-reduction-311b
} if inSequence('1' %1 %2 %3 %4)

replace restart {
	ld	%1 (ix),d
	ld	%2 (ix),e
	ld	%3 (ix),h
	ld	%4 (ix),l
} by {
	ld	bc,#%1
	call	____sdcc_store_dehl_bcix
	; peephole z88dk-code-reduction-312a
} if notUsed('bc'), inSequence('1' %4 %3 %2 %1)

replace restart {
	ld	%1 (ix),d
	ld	%2 (ix),e
	ld	%3 (ix),h
	ld	%4 (ix),l
} by {
	push	bc
	ld	bc,#%1
	call	____sdcc_store_dehl_bcix
	pop	bc
	; peephole z88dk-code-reduction-312b
} if notUsed('bc'), inSequence('1' %4 %3 %2 %1)

replace restart {
	ld	c,%1 (ix)
	ld	b,%2 (ix)
	ld	e,%3 (ix)
	ld	d,%4 (ix)
} by {
	ld	de,#%1
	call	____sdcc_load_debc_deix
	; peephole z88dk-code-reduction-313
} if inSequence('1' %1 %2 %3 %4)

replace restart {
	ld	l,%1 (ix)
	ld	h,%2 (ix)
	ld	e,%3 (ix)
	ld	d,%4 (ix)
} by {
	ld	de,#%4
	call	____sdcc_load_dehl_deix
	; peephole z88dk-code-reduction-314
} if notUsed('a'), inSequence('1' %1 %2 %3 %4)

replace restart {
	ld	%1 (ix),c
	ld	%2 (ix),b
	ld	%3 (ix),e
	ld	%4 (ix),d
} by {
	ld	hl,#%4
	call	____sdcc_store_debc_hlix
	; peephole z88dk-code-reduction-315a
} if notUsed('hl'), inSequence('1' %1 %2 %3 %4)

replace restart {
	ld	%1 (ix),c
	ld	%2 (ix),b
	ld	%3 (ix),e
	ld	%4 (ix),d
} by {
	push	hl
	ld	hl,#%4
	call	____sdcc_store_debc_hlix
	pop	hl
	; peephole z88dk-code-reduction-315b
} if inSequence('1' %1 %2 %3 %4)


barrier


///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// Z88DK CODE SIZE REDUCTION FOR INT
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

replace restart {
	ld	a,(hl)
	ld	%1 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%2 (ix),a
} by {
	ld	de,#%1
	call	____sdcc_2_copy_src_mhl_dst_deix
	; peephole z88dk-code-reduction-500a
} if notUsed('de')

replace restart {
	ld	a,(hl)
	ld	%1 (ix),a
	inc	hl
	ld	a,(hl)
	ld	%2 (ix),a
} by {
	ld	bc,#%1
	call	____sdcc_2_copy_src_mhl_dst_bcix
	; peephole z88dk-code-reduction-500b
} if notUsed('bc')


barrier



///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// Z88DK INTRINSICS
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

replace restart {
	call	_SECTION_SIZE_CODE
} by {
	nop	; EXTERN __CODE_END_tail
	nop	; EXTERN __CODE_head
	ld	hl,#(__CODE_END_tail - __CODE_head)
	; peephole z88dk-intrinsic-section-size
}

replace restart {
	call	_SECTION_SIZE_DATA
} by {
	nop	; EXTERN __DATA_END_tail
	nop	; EXTERN __DATA_head
	ld	hl,#(__DATA_END_tail - __DATA_head)
	; peephole z88dk-intrinsic-section-size
}

replace restart {
	call	_SECTION_SIZE_BSS
} by {
	nop	; EXTERN __BSS_UNINITIALIZED_head
	nop	; EXTERN __BSS_head
	ld	hl,#(__BSS_UNINITIALIZED_head - __BSS_head)
	; peephole z88dk-intrinsic-section-size
}

replace restart {
	call	_SECTION_ORG_%1
} by {
	nop	; EXTERN __%1_head
	ld	hl,#__%1_head
	; peephole z88dk-intrinsic-section-org
}

replace restart {
	call	_SECTION_END_%1
} by {
	nop	; EXTERN __%1_tail
	ld	hl,#__%1_tail
	; peephole z88dk-intrinsic-section-end
}

replace restart {
	call	_SECTION_SIZE_%1
} by {
	nop	; EXTERN __%1_size
	ld	hl,#__%1_size
	; peephole z88dk-intrinsic-section-size
}

replace restart {
	call	_intrinsic_outi_num_256
} by {
	call	____sdcc_outi_256
	; peephole z88dk-intrinsic-outi-num-256
}

replace restart {
	jp	_intrinsic_outi_num_256
} by {
	jp	____sdcc_outi_256
	; peephole z88dk-intrinsic-outi-num-256
}

replace restart {
	call	_intrinsic_outi_num_128
} by {
	call	____sdcc_outi_128
	; peephole z88dk-intrinsic-outi-num-128
}

replace restart {
	jp	_intrinsic_outi_num_128
} by {
	jp	____sdcc_outi_128
	; peephole z88dk-intrinsic-outi-num-128
}

barrier

replace restart {
	call	_intrinsic_outi_num_%1
} by {
	call	____sdcc_outi-(%1*2)
	; peephole z88dk-intrinsic-outi-num-%1
}

replace restart {
	jp	_intrinsic_outi_num_%1
} by {
	jp	____sdcc_outi-(%1*2)
	; peephole z88dk-intrinsic-outi-num-%1
}

replace restart {
	call	_intrinsic_outi_port_%1
} by {
	ld	c,#%1
	; peephole z88dk-intrinsic-outi-port-%1
}

replace restart {
	call	_intrinsic_outi
} by {
	; peephole z88dk-intrinsic-outi
}

barrier

replace restart {
	call	_intrinsic_ini_num_512
} by {
	call	____sdcc_ini_512
	; peephole z88dk-intrinsic-ini-num-512
}

replace restart {
	jp	_intrinsic_ini_num_512
} by {
	jp	____sdcc_ini_512
	; peephole z88dk-intrinsic-ini-num-512
}

replace restart {
	call	_intrinsic_ini_num_256
} by {
	call	____sdcc_ini_256
	; peephole z88dk-intrinsic-ini-num-256
}

replace restart {
	jp	_intrinsic_ini_num_256
} by {
	jp	____sdcc_ini_256
	; peephole z88dk-intrinsic-ini-num-256
}

replace restart {
	call	_intrinsic_ini_num_128
} by {
	call	____sdcc_ini_128
	; peephole z88dk-intrinsic-ini-num-128
}

replace restart {
	jp	_intrinsic_ini_num_128
} by {
	jp	____sdcc_ini_128
	; peephole z88dk-intrinsic-ini-num-128
}

barrier

replace restart {
	call	_intrinsic_ini_num_%1
} by {
	call	____sdcc_ini-(%1*2)
	; peephole z88dk-intrinsic-ini-num-%1
}

replace restart {
	jp	_intrinsic_ini_num_%1
} by {
	jp	____sdcc_ini-(%1*2)
	; peephole z88dk-intrinsic-ini-num-%1
}

replace restart {
	call	_intrinsic_ini_port_%1
} by {
	ld	c,#%1
	; peephole z88dk-intrinsic-ini-port-%1
}

replace restart {
	call	_intrinsic_ini
} by {
	; peephole z88dk-intrinsic-ini
}

barrier

replace restart {
	call	_intrinsic_ldi_num_256
} by {
	call	____sdcc_ldi_256
	; peephole z88dk-intrinsic-ldi-num-256
}

replace restart {
	jp	_intrinsic_ldi_num_256
} by {
	jp	____sdcc_ldi_256
	; peephole z88dk-intrinsic-ldi-num-256
}

replace restart {
	call	_intrinsic_ldi_num_128
} by {
	call	____sdcc_ldi_128
	; peephole z88dk-intrinsic-ldi-num-128
}

replace restart {
	jp	_intrinsic_ldi_num_128
} by {
	jp	____sdcc_ldi_128
	; peephole z88dk-intrinsic-ldi-num-128
}

barrier

replace restart {
	call	_intrinsic_ldi_num_%1
} by {
	call	____sdcc_ldi-(%1*2)
	; peephole z88dk-intrinsic-ldi-num-%1
}

replace restart {
	jp	_intrinsic_ldi_num_%1
} by {
	jp	____sdcc_ldi-(%1*2)
	; peephole z88dk-intrinsic-ldi-num-%1
}

replace restart {
	call	_intrinsic_ldi
} by {
	pop	de
	pop	hl
	; peephole z88dk-intrinsic-ldi
}

replace restart {
	call	____sdcc_ldi%1
	ret
} by {
	jp	____sdcc_ldi%1
	; peephole z88dk-intrinsic-ldi-tailcall
}

barrier

replace restart {
	call	_intrinsic_slp
} by {
	slp
	; peephole z88dk-intrinsic-slp
}

replace restart {
	jp	_intrinsic_slp
} by {
	slp
	ret
	; peephole z88dk-intrinsic-slp
}

replace restart {
	call	_intrinsic_stub
} by {
	; peephole z88dk-intrinsic-stub
}

replace restart {
	jp	_intrinsic_stub
} by {
	ret
	; peephole z88dk-intrinsic-stub
}

replace restart {
	call	_intrinsic_di
} by {
	di
	; peephole z88dk-intrinsic-di
}

replace restart {
	jp	_intrinsic_di
} by {
	di
	ret
	; peephole z88dk-intrinsic-di
}

replace restart {
	call	_intrinsic_ei
} by {
	ei
	; peephole z88dk-intrinsic-ei
}

replace restart {
	jp	_intrinsic_ei
} by {
	ei
	ret
	; peephole z88dk-intrinsic-ei
}

replace restart {
	call	_intrinsic_halt
} by {
	halt
	; peephole z88dk-intrinsic-halt
}

replace restart {
	jp	_intrinsic_halt
} by {
	halt
	ret
	; peephole z88dk-intrinsic-halt
}

replace restart {
	call	_intrinsic_label_%1
} by {
%1::
	; peephole z88dk-intrinsic-label %1
}

replace restart {
	jp	_intrinsic_label_%1
} by {
%1::
	ret
	; peephole z88dk-intrinsic-label %1
}

replace restart {
	call	_intrinsic_load16_%1
} by {
	ld	hl,(#%1)
	; peephole z88dk-intrinsic-load (%1)
}

replace restart {
	jp	_intrinsic_load16_%1
} by {
	ld	hl,(#%1)
	ret
	; peephole z88dk-intrinsic-load (%1)
}

replace restart {
	call	_intrinsic_store16_address_%1
	call	_intrinsic_store16_value_%2
} by {
	ld	hl,#%2
	ld	(#%1),hl
	; peephole z88dk-intrinsic-store (%1),%2
} 

replace restart {
	call	_intrinsic_reti
} by {
	reti
	; peephole z88dk-intrinsic-reti
}

replace restart {
	jp	_intrinsic_reti
} by {
	reti
	; peephole z88dk-intrinsic-reti
}

replace restart {
	call	_intrinsic_retn
} by {
	retn
	; peephole z88dk-intrinsic-retn
}

replace restart {
	jp	_intrinsic_retn
} by {
	retn
	; peephole z88dk-intrinsic-retn
}

replace restart {
	call	_intrinsic_im_0
} by {
	im	0
	; peephole z88dk-intrinsic-im-0
}

replace restart {
	jp	_intrinsic_im_0
} by {
	im	0
	ret
	; peephole z88dk-intrinsic-im-0
}

replace restart {
	call	_intrinsic_im_1
} by {
	im	1
	; peephole z88dk-intrinsic-im-1
}

replace restart {
	jp	_intrinsic_im_1
} by {
	im	1
	ret
	; peephole z88dk-intrinsic-im-1
}

replace restart {
	call	_intrinsic_im_2
} by {
	im	2
	; peephole z88dk-intrinsic-im-2
}

replace restart {
	jp	_intrinsic_im_2
} by {
	im	2
	ret
	; peephole z88dk-intrinsic-im-2
}

replace restart {
	call	_intrinsic_nop
} by {
	nop
	; peephole z88dk-intrinsic-nop
}

replace restart {
	jp	_intrinsic_nop
} by {
	nop
	ret
	; peephole z88dk-intrinsic-nop
}

replace restart {
	call	_intrinsic_exx
} by {
	exx
	; peephole z88dk-intrinsic-exx
}

replace restart {
	jp	_intrinsic_exx
} by {
	exx
	ret
	; peephole z88dk-intrinsic-exx
}

replace restart {
	call	_intrinsic_ex_de_hl
} by {
	ex	de,hl
	; peephole z88dk-intrinsic-ex-de-hl
}

replace restart {
	jp	_intrinsic_ex_de_hl
} by {
	ex	de,hl
	ret
	; peephole z88dk-intrinsic-ex-de-hl
}

replace restart {
	call	_intrinsic_return_bc
} by {
	ld	l,c
	ld	h,b
	; peephole z88dk-intrinsic-return-bc
}

replace restart {
	jp	_intrinsic_return_bc
} by {
	ld	l,c
	ld	h,b
	ret
	; peephole z88dk-intrinsic-return-bc
}

replace restart {
	call	_intrinsic_return_de
} by {
	ld	l,e
	ld	h,d
	; peephole z88dk-intrinsic-return-de
}

replace restart {
	jp	_intrinsic_return_de
} by {
	ld	l,e
	ld	h,d
	ret
	; peephole z88dk-intrinsic-return-de
}

replace restart {
	call	_intrinsic_swap_endian_16_fastcall
} by {
	ld	a,l
	ld	l,h
	ld	h,a
	; peephole z88dk-intrinsic-swap-endian-16
}

replace restart {
	jp	_intrinsic_swap_endian_16_fastcall
} by {
	ld	a,l
	ld	l,h
	ld	h,a
	ret
	; peephole z88dk-intrinsic-swap-endian-16
}

replace restart {
	call	_intrinsic_swap_endian_32_fastcall
} by {
	ld	a,d
	ld	d,l
	ld	l,a
	ld	a,e
	ld	e,h
	ld	h,a
	; peephole z88dk-intrinsic-swap-endian-32
}

replace restart {
	jp	_intrinsic_swap_endian_32_fastcall
} by {
	ld	a,d
	ld	d,l
	ld	l,a
	ld	a,e
	ld	e,h
	ld	h,a
	ret
	; peephole z88dk-intrinsic-swap-endian-32
}

replace restart {
	call	_intrinsic_swap_word_32_fastcall
} by {
	ex	de,hl
	; peephole z88dk-intrinsic-swap-word-32
}

replace restart {
	jp	_intrinsic_swap_word_32_fastcall
} by {
	ex	de,hl
	ret
	; peephole z88dk-intrinsic-swap-word-32
}

replace restart {
	ret
	ret
} by {
	ret
	; peephole z88dk-intrinsic-retret
}


barrier


///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// Z88DK BUGFIXES APPLIED TO SDCC FOR #9570
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

replace restart {
	ld	(#%1),hl
	ld	(#%1 + 2),de
	ld	hl,#%1
	ld	l,(hl)
	ld	hl,#%1 + 1
	ld	h,(hl)
	ld	hl,#%1 + 2
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
} by {
	ld	(#%1),hl
	ld	(#%1 + 2),de
	; peephole z88dk-bugfix-1
}
// if notVolatile

replace restart {
	ld	hl,#%1
	ld	l,(hl)
	ld	hl,#%1 + 1
	ld	h,(hl)
	ld	hl,#%1 + 2
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
} by {
	ld	hl,(#%1)
	ld	de,(#%1 + 2)
	; peephole z88dk-bugfix-2
}

replace restart {
	ld	hl,#%1
	ld	hl,#%2
	ld	a,(hl)
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	ld	(hl),a
} by {
	ld	hl,#%1
	ld	a,(#%2)
	ld	(hl),a
	inc	hl
	ld	a,(#%2 + 1)
	ld	(hl),a
	ld	hl,#%2 + 1
	; peephole z88dk-bugfix-3a
}

replace restart {
	ld	hl,#%1
	ld	hl,#%2
	ld	a,(hl)
	ld	(hl),a
} by {
	ld	a,(#%2)
	ld	(#%1),a
	; peephole z88dk-bugfix-3b
}

replace restart {
	ld	a,i
	di
	push	af
} by {
	call	____sdcc_cpu_push_di
	; peephole z88dk-bugfix-4a-critical-open-1
}

replace restart {
	pop	af
	jp	PO,%1
	ei
%1:
} by {
	call	____sdcc_cpu_pop_ei
%1:
	; peephole z88dk-bugfix-4c-critical-close-1
}

replace restart {
	in	a,(%1)
	in	a,(%1)
	ld	l,a
	in	a,(%1)
	ld	h,a
} by {
	ld	hl,(%1)
	; peephole z88dk-bugfix-5a-sdcc-2523
}

replace restart {
	in	a,(%1)
	in	a,(%1)
	ld	e,a
	in	a,(%1)
	ld	d,a
} by {
	ld	de,(%1)
	; peephole z88dk-bugfix-5b-sdcc-2523
}

replace restart {
	in	a,(%1)
	in	a,(%1)
	ld	c,a
	in	a,(%1)
	ld	b,a
} by {
	ld	bc,(%1)
	; peephole z88dk-bugfix-5c-sdcc-2523
}

replace restart {
	ld	a,(hl)
	and	a,#%5
	jp	NZ,%1
	jp	%2
%1:
	jp	%3
%2:
	or	a, a
	jp	NZ,%3
} by {
	ld	a,(hl)
	and	a,#%5
	jp	NZ,%1
	jp	%2
%1:
	jp	%3
%2:
	ld	a,(hl)
	and	a,#0x01
	jp	NZ,%3
	; peephole z88dk-bugfix-6a
}

replace restart {
	ld	a,(hl)
	and	a,#%5
	jp	NZ,%1
	jp	%2
%1:
	or	a, a
	jp	NZ,%2
} by {
	ld	a,(hl)
	and	a,#%5
	jp	NZ,%1
	jp	%2
%1:
	ld	a,(hl)
	and	a,#0x01
	jp	NZ,%2
	; peephole z88dk-bugfix-6b
}

replace restart {
	ld	hl,#%1
	ld	(hl),l
	inc	hl
	ld	(hl),h
} by {
	ld	(#%1),hl
	ld	hl,#%1 + 1
	; peephole z88dk-bugfix-7a
}

replace restart {
	ld	hl,#%1 + %2
	ld	(hl),l
	inc	hl
	ld	(hl),h
} by {
	ld	(#%1+%2),hl
	ld	hl,#%1 + %2 + 1
	; peephole z88dk-bugfix-7b
}

replace restart {
	ld	hl,#%1
	ld (hl),l
} by {
	ld	a,l
	ld	(#%1),a
	; peephole z88dk-bugfix-8a1
} if notUsed('a'), notUsed('hl')

replace restart {
	ld	hl,#%1
	ld (hl),l
} by {
	ld	a,l
	ld	hl,#%1
	ld	(hl),a
	; peephole z88dk-bugfix-8a2
} if notUsed('a')

replace restart {
	ld	hl,#%1
	ld	(hl),l
} by {
	ld	e,l
	ld	hl,#%1
	ld	(hl),e
	; peephole z88dk-bugfix-8a3
} if notUsed('e')

replace restart {
	ld	hl,#%1
	ld	(hl),l
} by {
	ld	c,l
	ld	hl,#%1
	ld	(hl),c
	; peephole z88dk-bugfix-8a4
} if notUsed('c')

replace restart {
	ld	hl,#%1 + %2
	ld (hl),l
} by {
	ld	a,l
	ld	(#%1 + %2),a
	; peephole z88dk-bugfix-8b1
} if notUsed('a'), notUsed('hl')

replace restart {
	ld	hl,#%1 + %2
	ld (hl),l
} by {
	ld	a,l
	ld	hl,#%1 + %2
	ld	(hl),a
	; peephole z88dk-bugfix-8b2
} if notUsed('a')

replace restart {
	ld	hl,#%1 + %2
	ld	(hl),l
} by {
	ld	e,l
	ld	hl,#%1 + %2
	ld	(hl),e
	; peephole z88dk-bugfix-8b3
} if notUsed('e')

replace restart {
	ld	hl,#%1 + %2
	ld	(hl),l
} by {
	ld	c,l
	ld	hl,#%1 + %2
	ld	(hl),c
	; peephole z88dk-bugfix-8b4
} if notUsed('c')

replace restart {
	ld	hl,#%1
	ld	(hl),h
	inc	hl
	ld	(hl),l
} by {
	ld	a,l
	ld	l,h
	ld	h,a
	ld	(#%1),hl
	ld	hl,#%1 + 1
	; peephole z88dk-bugfix-9a
} if notUsed('a')

replace restart {
	ld	hl,#%1 + %2
	ld	(hl),h
	inc	hl
	ld	(hl),l
} by {
	ld	a,l
	ld	l,h
	ld	h,a
	ld	(#%1+%2),hl
	ld	hl,#%1 + %2 + 1
	; peephole z88dk-bugfix-9b
} if notUsed('a')

replace restart {
	ld	hl,#%1
	ld	(hl),%2
	inc	hl
	ld	(hl),l
} by {
	ld	h,l
	ld	l,%2
	ld	(#%1),hl
	ld	hl,#%1 + 1
	; peephole z88dk-bugfix-10a
} if same(%2 'a' 'b' 'c' 'd' 'e')

replace restart {
	ld	hl,#%1 + %3
	ld	(hl),%2
	inc	hl
	ld	(hl),l
} by {
	ld	h,l
	ld	l,%2
	ld	(#%1+%3),hl
	ld	hl,#%1 + %3 + 1
	; peephole z88dk-bugfix-10b
} if same(%2 'a' 'b' 'c' 'd' 'e')

replace restart {
	ld	hl,#%1
	ld	a,(hl)
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	ld	(hl),a
} by {
	ld	e,l
	ld	d,h
	ld	hl,#%1
	ldi
	ldi
	ldi
	ld	a,(hl)
	ld	(de),a
	inc	bc
	inc	bc
	inc	bc
	; peephole z88dk-bugfix-11a
} if notUsed('de')

replace restart {
	ld	hl,#%1
	ld	a,(hl)
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	ld	(hl),a
} by {
	push	de
	ld	e,l
	ld	d,h
	ld	hl,#%1
	ldi
	ldi
	ldi
	ld	a,(hl)
	ld	(de),a
	inc	bc
	inc	bc
	inc	bc
	pop	de
	; peephole z88dk-bugfix-11b
}

// z88dk-bugfix-12a and z88dk-bugfix-12b removed in #1811

replace restart
{
	push	hl
	pop	iy
	push	hl
	push	bc
	push	de
	push	iy
	pop	de
	pop	hl
	pop	bc
	pop	hl
	push	hl
} by {
	push	hl
	push	hl
	pop	iy
	ex	de,hl
	; peephole z88dk-bugfix-13
}

replace restart {
	ld	a,l
	ld	d,h
	ld	d,a
	ld	h,d
} by {
	ld	a,l
	ld	d,l
	; peephole z88dk-bugfix-14
}

barrier


///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// SDCC PEEPHOLE RULES EXCEPT JP->JR FROM #9570
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

// peeph-z80.def - Z80 peephole rules
//
// Some of these peepholes could be potentially moved to peeph.def, but a
// GBZ80 expert should have a look at them before.
//
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2012
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

replace restart {
	ld	%1, %1
} by {
	; peephole 0 removed redundant load from %1 into %1.
} if notVolatile(%1)

replace restart {
	ld	%1, %2
} by {
	; peephole 1 removed dead load from %2 into %1.
} if notVolatile(%1), notUsed(%1), notVolatile(%2)

replace restart {
	add	ix,sp
} by {
	; peephole 1a removed dead frame pointer setup.
} if notUsed('ix')

replace restart {
	ld	%1, %2 + %3
} by {
	; peephole 2 removed dead load from %2 + %3 into %1.
} if notVolatile(%1), notUsed(%1)
// Should probably check for notVolatile(%2), too, but gives many false positives and no regression tests fail.

replace restart {
	ld	%1, (iy)
} by {
	ld	%1, 0 (iy)
	; peephole 3 made 0 offset explicit.
}

replace restart {
	ld	(iy), %1
} by {
	ld	0 (iy), %1
	; peephole 4 made 0 offset explicit.
}

replace restart {
	inc	hl
} by {
	; peephole 5 removed dead increment of hl.
} if notUsed('hl')

replace restart {
	dec	hl
} by {
	; peephole 6 removed dead decrement of hl.
} if notUsed('hl')

replace restart {
	ld	%1, %2 (iy)
} by {
	; peephole 7 removed dead load from %2 (iy) into %1.
} if notUsed(%1)
// Should probably check for notVolatile(), but gives many false positives and no regression tests fail.

replace restart {
	ld	%1, %2 (ix)
} by {
	; peephole 8 removed dead load from %2 (ix) into %1.
} if notUsed(%1)

replace restart {
	ld	%1, %2
	ld	%3, %1
} by {
	; peephole 9 loaded %3 from %2 directly instead of going through %1.
	ld	%3, %2
} if canAssign(%3 %2), notVolatile(%1), notUsed(%1)

replace restart {
	ld	%1, %2
	ld	%3, %4
	ld	%5, %1
} by {
	ld	%5, %2
	; peephole 10 loaded %5 from %2 directly instead of going through %1.
	ld	%3, %4
} if notUsed(%1), canAssign(%5 %2), notVolatile(%1), notVolatile(%5), operandsNotRelated(%1 %3), operandsNotRelated(%1 %4), operandsNotRelated(%2 %3), operandsNotRelated(%3 %5), operandsNotRelated(%4 %5), notSame(%3 %4 '(hl)' '(de)' '(bc)')
// Rule OK unless both %5 and %4 are volatile, but we can't express that directly.

replace restart {
	ld	%1, %2 (%3)
	ld	%4, %1
} by {
	; peephole 11 loaded %2 (%3) into %4 directly instead of going through %1.
	ld	%4, %2 (%3)
} if canAssign(%4 %2 %3), notVolatile(%1), notUsed(%1)

replace restart {
	ld	%1, %2
	ld	%3 (%4), %1
} by {
	; peephole 12 loaded %2 into %3 (%4) directly instead of going through %1.
	ld	%3 (%4), %2
} if canAssign(%3 %4 %2), notVolatile(%1), notUsed(%1)

replace restart {
	ld	%1, %2 (%3)
	ld	%4, %5 (%6)
	ld	%7, %1
} by {
	ld	%7, %2 (%3)
	; peephole 13 loaded %2 (%3) into %7 directly instead of going through %1.
	ld	%4, %5 (%6)
} if canAssign(%7 %2 %3), notVolatile(%1), notUsed(%1), notSame(%1 %4), notSame(%7 %4)

replace restart {
	ld	%1, %7
	ld	%5 (%6), %4
	ld	%2 (%3), %1
} by {
	ld	%5 (%6), %4
	; peephole 14 loaded %7 into %2 (%3) directly instead of going through %1.
	ld	%2 (%3), %7
} if canAssign(%2 %3 %7), notVolatile(%1), notUsed(%1), notSame(%1 %4)

replace restart {
	ld	%1, %2 (%3)
	ld	%4, %5
	ld	%7, %1
} by {
	ld	%7, %2 (%3)
	; peephole 15 loaded %2 (%3) into %7 directly instead of going through %1.
	ld	%4, %5
} if canAssign(%7 %2 %3), notVolatile(%1), notUsed(%1), operandsNotRelated(%1 %5), operandsNotRelated(%7 %4), operandsNotRelated(%7 %5), notSame(%4 '(hl)' '(de)' '(bc)'), notSame(%5 '(hl)' '(de)' '(bc)' '(iy)')

replace restart {
	ld	%1,#%2
	ld	a,%3 (%1)
} by {
	; peephole 16 loaded %2 into a directly instead of going through %1.
	ld	a,(#%2 + %3)
} if notUsed(%1)

replace restart {
	ld	hl,#%1
	ld	a,(hl)
} by {
	ld	a,(#%1)
	; peephole 17 loaded a from (#%1) directly instead of using hl.
} if notUsed('hl')

replace restart {
	ld	hl,#%1 + %2
	ld	a,(hl)
} by {
	; peephole 18 loaded %2 into a directly instead of using hl.
	ld	a,(#%1 + %2)
} if notUsed('hl')

replace restart {
	ld	hl,#%1
	ld	(hl),a
} by {
	ld	(#%1),a
	; peephole 19 loaded (#%1) from a directly instead of using hl.
} if notUsed('hl')

replace restart {
	ld	hl,#%1 + %2
	ld	(hl),a
} by {
	ld	(#%1 + %2),a
	; peephole 20 loaded (#%1) from a directly instead of using hl.
} if notUsed('hl')

replace restart {
	srl	%1
	ld	a,%1
} by {
	ld	a,%1
	; peephole 21 shifted in a instead of %1.
	srl	a
} if notVolatile(%1), notUsed(%1)

replace restart {
	ld	e, l
	ld	d, h
	ld	a, (de)
	srl	a
	ld	(de), a
} by {
	ld	e, l
	ld	d, h
	srl	(hl)
	ld	a, (hl)
	; peephole 21a shifted in (hl) instead of a.
}

replace restart {
	ld	a, %1 (%2)
	srl	a
	ld	%1 (%2), a
} by {
	srl	%1 (%2)
	ld	a, %1 (%2)
	; peephole 21b shifted in (%2) instead of a.
}

replace restart {
	ld	iy, #%1
	ld	%2, %3 (iy)
	srl	%2
	bit %4, %3 (iy)
} by {
	ld	hl, #%1 + %3
	; peephole 21c used hl instead of iy.
	ld	%2, (hl)
	srl %2
	bit %4, (hl)
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	%1,(hl)
	ld	a,%2 (%3)
	sub	a,%1
} by {
	ld	a,%2 (%3)
	; peephole 22 used (hl) in sub directly instead of going through %1.
	sub	a,(hl)
} if notVolatile(%1), notUsed(%1)

replace restart {
	inc	bc
	ld	l,c
	ld	h,b
} by {
	ld	l,c
	ld	h,b
	; peephole 23 incremented in hl instead of bc.
	inc	hl
} if notUsed('bc')

replace restart {
	inc	de
	ld	l,e
	ld	h,d
} by {
	ld	l,e
	ld	h,d
	; peephole 24 incremented in hl instead of de.
	inc	hl
} if notUsed('de')

replace restart {
	ld	c,l
	ld	b,h
	ld	a,#%1
} by {
	ld	a,#%1
	ld	c,l
	ld	b,h
	; peephole 25a delay copy of hl into bc
}

replace restart {
	ld	c,l
	ld	b,h
	ld	(bc),a
} by {
	ld	(hl),a
	ld	c,l
	ld	b,h
	; peephole 25b loaded a into (hl) instead of (bc)
}

replace restart {
	ld	a,#%1
	ld	(hl),a
} by {
	ld	(hl),#%1
	; peephole 25c loaded #%1 into (hl) directly
} if notUsed('a')

replace restart {
	ex	de, hl
	push	de
} by {
	; peephole 26 pushed hl directly instead of going through de.
	push	hl
} if notUsed('de'), notUsed('hl')

replace restart {
	ld	l,%1
	ld	h,d
	push	hl
} by {
	; peephole 27 pushed de instead of hl removing a load.
	ld	e,%1
	push	de
} if notUsed('hl'), notUsed('e')

replace restart {
	ex	de, hl
	push	bc
	push	de
} by {
	; peephole 28 pushed hl directly instead of going through de.
	push	bc
	push	hl
} if notUsed('de'), notUsed('hl')

replace restart {
	ld	l,c
	ld	h,b
	push	hl
} by {
	; peephole 29 pushed bc directly instead of going through hl.
	push	bc
} if notUsed('hl')

replace restart {
	ld	l,%1
	ld	h,b
	push	hl
} by {
	; peephole 30 pushed bc instead of hl removing a load.
	ld	c,%1
	push	bc
} if notUsed('hl'), notUsed('c')

replace restart {
	ld	c,l
	ld	b,h
	push	%1
	push	bc
} by {
	; peephole 31 pushed hl directly instead of going through bc.
	push	%1
	push	hl
} if notUsed('bc'), notSame(%1 'bc')

replace restart {
	pop	de
	ld	l, e
	ld	h, d
} by {
	; peephole 32 popped hl directly instead of going through de.
	pop	hl
} if notUsed('de')

replace restart {
	pop	bc
	ld	l, c
	ld	h, b
} by {
	; peephole 33 popped hl directly instead of going through bc.
	pop	hl
} if notUsed('bc')

replace restart {
	ld	%1 (ix), %2
	ld	%3, %1 (ix)
} by {
	; peephole 34 loaded %3 from %2 instead of going through %1 (ix).
	ld	%1 (ix), %2
	ld	%3, %2
}
// Don't need to check for volatile, since ix is used for the stack.

replace restart {
	ld	%1 (ix), a
	push	de
	ld	%2, %1 (ix)
} by {
	ld	%1 (ix), a
	push	de
	; peephole 34a loaded %2 from a instead of %1 (ix)
	ld	%2, a
}	

replace restart {
	push	af
	inc	sp
	ld	a,e
	push	af
	inc	sp
} by {
	; peephole 35 pushed de instead of pushing a twice.
	ld	d,a
	push	de
} if notUsed('d'), notUsed('a')

replace restart {
	push	af
	inc	sp
	ld	a,#%1
	push	af
	inc	sp
} by {
	; peephole 36 pushed de instead of pushing a twice.
	ld	d,a
	ld	e,#%1
	push	de
} if notUsed('de')

replace restart {
	push	af
	inc	sp
	ld	a,#%1
	push	af
	inc	sp
} by {
	; peephole 37 pushed bc instead of pushing a twice.
	ld	b,a
	ld	c,#%1
	push	bc
} if notUsed('bc')

replace restart {
	push	bc
	inc	sp
	push	de
	inc	sp
} by {
	ld	c, d
	; peephole 37a combined pushing of b and d.
	push	bc
} if notUsed('c')

replace restart {
	push	bc
	inc	sp
	ld	a, c
	push	af
	inc	sp
} by {
	push	bc
	ld	a, c
	; peephole 38 simplified pushing bc.
}

replace restart {
	push	de
	inc	sp
	ld	a, #%1
	push	af
	inc	sp
} by {
	ld	e, #%1
	push	de
	; peephole 39 simplified pushing de.
} if notUsed('e')

replace restart {
	ld	a,#%1
	ld	d,a
} by {
	; peephole 40 loaded #%1 into d directly instead of going through a.
	ld	d,#%1
} if notUsed('a')

replace restart {
	ld	%1,a
	ld	%2,%1
} by {
	; peephole 41 loaded %2 from a directly instead of going through %1.
	ld	%2,a
} if notUsed(%1)

replace restart {
	ld	a,%1 (%3)
	push	af
	inc	sp
	ld	a,%2 (%3)
	push	af
	inc	sp
} by {
	; peephole 42 pushed %1 (%3), %2(%3) through hl instead of af.
	ld	h,%1 (%3)
	ld	l,%2 (%3)
	push	hl
} if notUsed('a'), notUsed('hl')

replace restart {
	ld	c, l
	ld	b, h
	push	bc
} by {
	; peephole 43 pushed hl instead of bc.
	push	hl
} if notUsed('bc')

replace restart {
	ld	a, (hl)
	inc	hl
	ld	h, (hl)
	ld	l, a
	push	hl
} by {
	; peephole 43a pushed bc instead of hl.
	ld	c, (hl)
	inc	hl
	ld	b, (hl)
	push	bc
} if notUsed('bc'), notUsed('hl')

replace restart {
	pop	%1
	push	%1
} by {
	; peephole 44 eleminated dead pop/push pair.
} if notUsed(%1)

replace restart {
	push	hl
	pop	bc
} by {
	ld	c, l
	ld	b, h
	; peephole 44a replaced push/pop pair by loads.
} if notUsed('hl')

replace restart {
	push	hl
	pop	de
} by {
	ld	e, l
	ld	d, h
	; peephole 44b replaced push/pop pair by loads.
} if notUsed('hl')

replace restart {
	ld	iy,#%1
	or	a,%2 (iy)
} by {
	; peephole 45 used hl instead of iy.
	ld	hl,#%1 + %2
	or	a,(hl)
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy,#%1
	ld	%2,%3 (iy)
} by {
	; peephole 46 used hl instead of iy.
	ld	hl,#%1 + %3
	ld	%2, (hl)
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy,#%1
	ld	h,%3 (iy)
} by {
	; peephole 46a used hl instead of iy.
	ld	hl,#%1 + %3
	ld	h, (hl)
} if notUsed('iy'), notUsed('l')

replace restart {
	ld	iy,#%1
	ld	%2 (iy), %3
} by {
	; peephole 46b used hl instead of iy.
	ld	hl,#%1 + %2
	ld	(hl), %3
} if notUsed('iy'), notUsed('hl'), notSame(%3 'h' 'l')

replace restart {
	ld	iy,#%1
	ld	%2,0 (iy)
	ld	%3,1 (iy)
} by {
	; peephole 47 used hl instead of iy.
	ld	hl,#%1
	ld	%2, (hl)
	inc	hl
	ld	%3, (hl)
} if notUsed('iy'), notUsed('hl'), operandsNotRelated(%2 'hl')

replace restart {
	ld	iy,#%1
	ld	%2 (iy),%3
	ld	l,%2 (iy)
} by {
	; peephole 48 used hl instead of iy.
	ld	hl,#%1 + %2
	ld	(hl),%3
	ld	l,(hl)
} if notUsed('iy'), notUsed('h')

replace restart {
	ld	iy,#%1
	ld	%2 (%3), %4
} by {
	; peephole 49 used hl instead of iy.
	ld	hl,#%1 + %2
	ld	(hl), %4
} if notUsed('iy'), notUsed('hl'), operandsNotRelated(%4 'hl')

replace restart {
	ld	iy,#%1
	bit	%2,%3 (iy)
} by {
	; peephole 49a used hl instead of iy.
	ld	hl,#%1+%3
	bit	%2, (hl)
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy, #%1
	add	iy, sp
	ld	%2, %3 (iy)
} by {
	; peephole 49b used hl instead of iy.
	ld	hl, #%1+%3
	add	hl, sp
	ld	%2, (hl)
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy, #%1
	add	iy, sp
	ld	%2, 0 (iy)
	ld	%3, 1 (iy)
} by {
	; peephole 49c used hl instead of iy.
	ld	hl, #%1
	add	hl, sp
	ld	%2, (hl)
	inc	hl
	ld	%3, (hl)
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy, #%1
	add	iy, sp
	ld	l, 0 (iy)
	ld	h, 1 (iy)
} by {
	; peephole 49d used hl instead of iy.
	ld	hl, #%1
	add	hl, sp
	ld	a, (hl)
	inc	hl
	ld	h, (hl)
	ld	l, a
} if notUsed('iy'), notUsed('a')

replace restart {
	ld	iy, #%1
	add	iy, sp
	ld	0 (iy), #%2
	ld	1 (iy), #%3
} by {
	; peephole 49e used hl instead of iy.
	ld	hl, #%1
	add	hl, sp
	ld	(hl), #%2
	inc	hl
	ld	(hl), #%3
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy, #%1+1
	ld	a, 1 (iy)
	or	a, 0 (iy)
} by {
	ld	hl, #%1
	ld	a, (hl)
	dec	hl
	or	a, (hl)
	; peephole 49f used hl instead of iy.
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy, #%1
	add	iy, sp
	ld	a, 1 (iy)
	or	a, 0 (iy)
} by {
	ld	hl, #%1+1
	add	hl, sp
	ld	a, (hl)
	dec	hl
	or	a, (hl)
	; peephole 49f' used hl instead of iy.
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy, #%1
	add	iy, sp
	bit	%2, %3 (iy)
} by {
	ld	hl, #%1+%3
	add	hl, sp
	bit	%2, (hl)
	; peephole 49g used hl instead of iy.
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	iy, #%1
	add	iy, sp
	or	a, %2 (iy)
} by {
	ld	hl, #%1+%2
	add	hl, sp
	or	a, (hl)
	; peephole 49h used hl instead of iy.
} if notUsed('iy'), notUsed('hl')

replace restart {
	ld	c,l
	ld	b,h
	inc	bc
} by {
	; peephole 51 incremented in hl instead of bc.
	inc	hl
	ld	c,l
	ld	b,h
} if notUsed('hl')

replace restart {
	ld	iy, #%1
	inc	%2 (iy)
} by {
	; peephole 51a incremented in (hl) instead of %2 (iy).
	ld	hl, #%1+%2
	inc	(hl)
} if notUsed('hl'), notUsed('iy')

replace restart {
	ld	iy, #%1
	dec	%2 (iy)
} by {
	; peephole 51a' decremented in (hl) instead of %2 (iy).
	ld	hl, #%1+%2
	dec	(hl)
} if notUsed('hl'), notUsed('iy')

replace restart {
	add	hl, hl
	ld	e, l
	ld	d, h
	inc	de
	inc	de
} by {
	inc	hl
	; peephole 51b incremented once in hl instead of incrementing in de twice.
	add	hl, hl
	ld	e, l
	ld	d, h
} if notUsed('hl')

replace restart {
	add	hl, hl
	inc	hl
	inc	hl
} by {
	inc	hl
	; peephole 51c incremented once in hl instead of incrementing in hl twice.
	add	hl, hl
}

replace restart {
	add	a, a
	add	a, a
	add	a, #0x04
} by {
	inc	a
	; peephole 51d incremented a once instead of adding #0x04 to a.
	add	a, a
	add	a, a
}

replace restart {
	add	hl, hl
	pop	de
	inc	hl
	inc	hl
} by {
	inc	hl
	; peephole 51e incremented once in hl instead of incrementing in hl twice.
	add	hl, hl
	pop	de
}

replace restart {
	ld	a,%1 (%2)
	bit	%3,a
} by {
	; peephole 52 tested bit of %1 (%2) directly instead of going through a.
	bit	%3,%1 (%2)
} if notUsed('a')

replace restart {
	ld	a,%1
	bit	%2,a
} by {
	; peephole 53 tested bit %2 of %1 directly instead of going through a.
	bit	%2,%1
} if notUsed('a'), canAssign(%1 'b')

replace restart {
	ld	a, %1
	set	%2, a
	ld	%1, a
} by {
	; peephole 54 set bit %2 of %1 directly instead of going through a.
	set	%2, %1
	ld	a, %1
} if canAssign(%1 'b')
// canAssign(%1 'b') is true, iff set b, %1 is possible.

replace restart {
	ld	a, %1 (%2)
	set	%3, a
	ld	%1 (%2), a
} by {
	; peephole 55 set bit %3 of %1 (%2) directly instead of going through a.
	set	%3, %1 (%2)
	ld	a, %1 (%2)
}

replace restart {
	ld	a, %1
	res	%2, a
	ld	%1, a
} by {
	; peephole 56 reset bit %2 of %1 directly instead of going through a.
	res	%2, %1
	ld	a, %1
} if canAssign(%1 'b')
// canAssign(%1 'b') is true, iff set b, %1 is possible.

replace restart {
	ld	a, %1 (%2)
	res	%3, a
	ld	%1 (%2), a
} by {
	; peephole 57 reset bit %3 of %1 (%2) directly instead of going through a.
	res	%3, %1 (%2)
	ld	a, %1 (%2)
}

replace restart {
	ld	c, %1 (%2)
	ld	b, %3 (%4)
	ld	l,c
	ld	h,b
} by {
	; peephole 58 stored %1 (%2) %3 (%4) into hl directly instead of going through bc.
	ld	l, %1 (%2)
	ld	h, %3 (%4)
} if notUsed('bc')

replace restart {
	ld	c, %1
	ld	b, %2
	ld	l,c
	ld	h,b
} by {
	; peephole 59 stored %2%1 into hl directly instead of going through bc.
	ld	l, %1
	ld	h, %2
} if notUsed('bc'), operandsNotRelated(%2 'l')

replace restart {
	jp	NC,%1
	jp	%2
%1:
} by {
	jp	C,%2
	; peephole 60 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)

replace restart {
	jp	C,%1
	jp	%2
%1:
} by {
	jp	NC,%2
	; peephole 61 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)

replace restart {
	jp	NZ,%1
	jp	%2
%1:
} by {
	jp	Z,%2
	; peephole 62 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)

replace restart {
	jp	Z,%1
	jp	%2
%1:
} by {
	jp	NZ,%2
	; peephole 63 removed jp by using inverse jump logic
%1:
} if labelRefCountChange(%1 -1)

replace restart {
	jp	%5
} by {
	jp	%6
	; peephole 64 jumped to %6 directly instead of via %5.
} if labelIsUncondJump(), notSame(%5 %6), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)

replace restart {
	jp	%1,%5
} by {
	jp	%1,%6
	; peephole 65 jumped to %6 directly instead of via %5.
} if labelIsUncondJump(), notSame(%5 %6), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)

replace restart {
	jp	%1
%2:
%1:
} by {
   ; peephole 65a eliminated jump.
%2:
%1:
} if labelRefCountChange(%1 -1)

// A peephole that makes the code longer. Let's hope it's worth it in speed gain and further optimization potential.
replace restart {
	ld	a,#0x00
%1:
	bit	%2,a
	jp	Z,%3
} by {
	ld	a,#0x00
	jp	%3
	; peephole 65a jumped directly to %3 instead of testing a first.
%1:
	bit	%2,a
	jp	Z,%3
} if labelRefCountChange(%3 +1)

replace restart {
	ld	%1, %2
	jp	%3
	jp	%4
} by {
	ld	%1, %2
	jp	%3
	; peephole 65b removed unreachable jump to %3.
}

replace restart {
	ld	%1, %2
	jp	%3
%3:
} by {
	ld	%1, %2
%3:
	; peephole 65c removed redundant jump to %3.
} if labelRefCountChange(%3 -1)

replace restart {
	ld	%1, #0x01
	bit	0, %1
	jp	Z, %2
} by {
	ld	%1, #0x01
	; peephole 65d removed impossible jump to %2.
} if labelRefCountChange(%2 -1)

replace restart {
	rlca
	and	a, #0x01
	jp	Z, %1
} by {
	rlca
	; peephole 65e removed and by changing jump condition.
	jp	NC, %1
} if notUsed('a'), notUsedFrom(%1 'a')

replace restart {
	rlca
	and	a, #0x01
	jp	NZ, %1
} by {
	rlca
	; peephole 65f removed and by changing jump condition.
	jp	C, %1
} if notUsed('a'), notUsedFrom(%1 'a')

replace restart {
	xor	a,a
	ld	a,#0x00
} by {
	xor	a,a
	; peephole 66 removed redundant load of 0 into a.
}

replace restart {
	ld	e,#0x%1
	ld	d,#0x%2
} by {
	ld	de,#0x%2%1
	; peephole 67 combined constant loads into register pair.
}

replace restart {
	ld	d,#0x%1
	ld	e,#0x%2
} by {
	ld	de,#0x%1%2
	; peephole 67a combined constant loads into register pair.
}

replace restart {
	ld	l,#0x%1
	ld	h,#0x%2
} by {
	ld	hl,#0x%2%1
	; peephole 68 combined constant loads into register pair.
}

replace restart {
	ld	h,#0x%1
	ld	l,#0x%2
} by {
	ld	hl,#0x%1%2
	; peephole 68a combined constant loads into register pair.
}

replace restart {
	ld	c,#0x%1
	ld	b,#0x%2
} by {
	ld	bc,#0x%2%1
	; peephole 69 combined constant loads into register pair.
}

replace restart {
	ld	b,#0x%1
	ld	c,#0x%2
} by {
	ld	bc,#0x%1%2
	; peephole 69a combined constant loads into register pair.
}

replace restart {
	ld	%1,a
	ld	a,%1
} by {
	ld	%1,a
	; peephole 70 removed redundant load from %1 into a.
} if notVolatile(%1)
// This gives many false negatives and without the test no problems are encountered in the regression tests
// Maybe we can try this after 2.7.0 release

replace restart {
	ld	a,%1
	ld	%1,a
} by {
	ld	a,%1
	; peephole 71 removed redundant load from a into %1.
} if notVolatile(%1)
// This gives many false negatives and without the test no problems are encountered in the regression tests
// Maybe we can try this after 2.7.0 release

replace restart {
	ld	%1,a
	ld	a,%2
	or	a,%1
} by {
	ld	%1,a
	or	a,%2
	; peephole 72 removed load by reordering or arguments.
} if notVolatile(%1), canAssign('b' %2)
// canAssign('b' %2) is true, iff or a,%2 is possible.

replace restart {
	or	a,%1
	or	a,a
} by {
	or	a,%1
	; peephole 73 removed redundant or after or.
}

replace restart {
	or	a,%1 (%2)
	or	a,a
} by {
	or	a,%1 (%2)
	; peephole 74 removed redundant or after or.
}

replace restart {
	and	a,%1
	or	a,a
} by {
	and	a,%1
	; peephole 75 removed redundant or after and.
}

replace restart {
	xor	a,%1
	or	a,a
} by {
	xor	a,%1
	; peephole 76 removed redundant or after xor.
}

replace restart {
	xor	a,%1 (%2)
	or	a,a
} by {
	xor	a,%1 (%2)
	; peephole 77 removed redundant or after xor.
}

replace restart {
	ld	%1,%2
	ld	a,%2
} by {
	ld	a,%2
	ld	%1,a
	; peephole 78 load value in a first and use it next
} if notVolatile(%1 %2)

replace restart {
	ld	%1,%2
	ld	%3,%4
	ld	%2,%1
	ld	%4,%3
} by {
	ld	%1,%2
	ld	%3,%4
	; peephole 79 removed redundant load from %3%1 into %4%2
} if notVolatile(%1 %2 %3 %4)

replace restart {
	push	de
	inc	sp
	ld	a,e
	push	af
	inc	sp
} by {
	push	de
	; peephole 80 pushed de
} if notUsed('a')

replace restart {
	ld	iy,%1
	add	iy,sp
	ld	sp,iy
} by {
	ld	hl,%1
	add	hl,sp
	ld	sp,hl
	; peephole 81 fixed stack using hl instead of iy.
} if notUsed('hl'), notUsed('iy')

replace restart {
	ld	a,%1
	sub	a,%2
	jp	%3,%4
	ld	a,%1
} by {
	ld	a,%1
	cp	a,%2
	jp	%3,%4
	; peephole 82 removed load from %1 into a by replacing sub with cp
	assert	a=%1
} if notVolatile(%1), notUsedFrom(%4 'a')

replace restart {
	assert	a=%1
	sub	a,%2
	jp	%3,%4
	ld	a,%1
} by {
	cp	a,%2
	jp	%3,%4
	; peephole 83 removed load from %1 into a by replacing sub with cp
	assert	a=%1
} if notUsedFrom(%4 'a')

replace restart {
	assert	a=%1
} by {
}

replace restart {
	sub	a,#0xFF
	jp	Z,%1
} by {
	inc	a
	; peephole 84 replaced sub a,#0xFF by inc a.
	jp	Z,%1
}

replace restart {
	sub	a,#0xFF
	jp	NZ,%1
} by {
	inc	a
	; peephole 85 replaced sub a,#0xFF by inc a.
	jp	NZ,%1
}

replace restart {
	rlca
	ld	a,#0x00
	rla
} by {
	rlca
	and	a,#0x01
	; peephole 86 replaced zero load, rla by and since rlca writes the same value to carry bit and least significant bit.
}

replace restart {
	ld	%1,%2
	push	%1
	pop	%4
	ld	%1,%3
} by {
	ld	%4,%2
	; peephole 87 moved %2 directly into de instead of going through %1.
	ld	%1,%3
}

replace restart {
	add	a,#0x00
	ld	%2,a
	ld	a,%3
	adc	a,%4
} by {
	; peephole 88 removed lower part of multibyte addition.
	ld	%2,a
	ld	a,%3
	add	a,%4
}

replace restart {
	ld	a, l
	add	a, #0x%1
	ld	e, a
	ld	a, h
	adc	a, #0x%2
	ld	d, a
} by {
	ld	de, #0x%2%1
	add	hl, de
	; peephole 89 used 16-bit addition.
	ld	e, l
	ld	d, h
	ld	a, h
} if notUsed('hl')

replace restart {
	ld	a, l
	add	a, #0x%1
	ld	c, a
	ld	a, h
	adc	a, #0x%2
	ld	b, a
} by {
	ld	bc, #0x%2%1
	add	hl,bc
	; peephole 90 used 16-bit addition.
	ld	c, l
	ld	b, h
	ld	a, h
} if notUsed('hl')

replace restart {
	ld	%1,a
	ld	a,%2
	add	a,%1
} by {
	; peephole 91 removed loads by exploiting commutativity of addition.
	add	a,%2
} if notVolatile(%1), notUsed(%1), canAssign('b' %2)
// canAssign('b' %2) is true, iff add a,%2 is possible.

replace restart {
	ld	%1 (ix),a
	ld	a,#%2
	add	a,%1 (ix)
} by {
	ld	%1 (ix),a
	; peephole 92 removed loads by exploiting commutativity of addition.
	add	a,#%2
}
// Don't need to check for volatile, since ix is used to access the stack.

replace restart {
	ld	l,%1 (ix)
	ld	h,%2 (ix)
	ld	a,(hl)
	inc	a
	ld	l,%1 (ix)
	ld	h,%2 (ix)
	ld	(hl),a
} by {
	ld	l,%1 (ix)
	ld	h,%2 (ix)
	inc	(hl)
	; peephole 93 incremented in (hl) instead of going through a.
} if notUsed('a')

replace restart {
	ld	a,(hl)
	inc	a
	ld	(hl),a
} by {
	inc	(hl)
	; peephole 93' incremented in (hl) instead of going through a.
} if notUsed('a')

// TODO: Check for volatile?
replace restart {
	ld	%1, %2 (%3)
	inc	%1
	ld	%2 (%3), %1
} by {
	inc	%2 (%3)
	ld	%1, %2 (%3)
	; peephole 93a incremented in %2 (%3) instead of going through %1.
}

// TODO: Check for volatile?
replace restart {
	ld	%1, %2 (%3)
	dec	%1
	ld	%2 (%3), %1
} by {
	dec	%2 (%3)
	ld	%1, %2 (%3)
	; peephole 93b decremented in %2 (%3) instead of going through %1.
}

replace restart {
	ld	%1,a
	ld	a,%2
	add	a,%1
} by {
	ld	%1, a
	; peephole 94 removed load by exploiting commutativity of addition.
	add	a,%2
} if notSame(%2 '(bc)' '(de)'), canAssign('b' %2)
// canAssign('b' %2) is true, iff add a,%2 is possible.

replace restart {
	ld	c,l
	ld	b,h
	ld	hl,#%1
	add	hl,bc
} by {
	; peephole 95 removed loads by exploiting commutativity of addition.
	ld	bc,#%1
	add	hl,bc
} if notUsed('bc')

replace restart {
	ld	hl,#%1
	add	hl,%2
	ld	bc,#%4
	add	hl,bc
} by {
	; peephole 96 removed loads by exploiting commutativity of addition.
	ld	hl,#%1 + %4
	add	hl,%2
} if notUsed('bc')

replace restart {
	ld	c,e
	ld	b,d
	ld	hl,#%1
	add	hl,bc
} by {
	; peephole 97 removed loads by exploiting commutativity of addition.
	ld	hl,#%1
	add	hl,de
} if notUsed('bc')

replace restart {
	or	a,%1
	jp	NZ,%2
	ld	%3,#0x00
} by {
	or	a,%1
	jp	NZ,%2
	ld	%3,a
	; peephole 98 replaced constant #0x00 by a (which has just been tested to be #0x00).
}

replace restart {
	and	a,%1
	jp	NZ,%2
	ld	%3,#0x00
} by {
	and	a,%1
	jp	NZ,%2
	ld	%3,a
	; peephole 99 replaced constant #0x00 by a (which has just been tested to be #0x00).
}

replace restart {
	sub	a,%1
	jp	NZ,%2
	ld	%3,#0x00
} by {
	sub	a,%1
	jp	NZ,%2
	ld	%3,a
	; peephole 100 replaced constant #0x00 by a (which has just been tested to be #0x00).
}

replace restart {
	inc	a
	jp	NZ,%1
	ld	%2,#0x00
} by {
	inc	a
	jp	NZ,%1
	ld	%2,a
	; peephole 101 replaced constant #0x00 by a (which has just been tested to be #0x00).
}

replace restart {
	dec	a
	jp	NZ,%1
	ld	%2,#0x00
} by {
	dec	a
	jp	NZ,%1
	ld	%2,a
	; peephole 102 replaced constant #0x00 by a (which has just been tested to be #0x00).
}

replace restart {
	or	a,%1
	jp	NZ,%2
	ld	a,%3
	or	a,a
} by {
	or	a,%1
	jp	NZ,%2
	or	a,%3
	; peephole 103 shortened or using a (which has just been tested to be #0x00).
} if canAssign('b' %3)
// canAssign('b' %2) is true, iff or a,%2 is possible.

replace restart {
	sub	a,%1
	jp	NZ,%2
	ld	a,%3
	or	a,a
} by {
	sub	a,%1
	jp	NZ,%2
	or	a,%3
	; peephole 104 shortened or using a (which has just been tested to be #0x00).
} if canAssign('b' %3)
// canAssign('b' %2) is true, iff or a,%2 is possible.

replace restart {
	or	a,%1
	jp	NZ,%2
	push	%3
	ld	%4,#0x00
} by {
	or	a,%1
	jp	NZ,%2
	push	%3
	ld	%4,a
	; peephole 105 replaced constant #0x00 by a (which has just been tested to be #0x00).
}

replace restart {
	ld	(hl),#0x00
	inc	hl
	ld	(hl),#0x00
} by {
	xor	a, a
	; peephole 106 cached zero in a.
	ld	(hl), a
	inc	hl
	ld	(hl), a
} if notUsed('a')

replace restart {
	ld	hl,#%1
	add	hl,%2
	inc	hl
} by {
	ld	hl,#%1+1
	add	hl,%2
	; peephole 107 moved increment of hl to constant.
}

replace restart {
	inc	hl
	ld	%1,#%2
	add	hl,%1
} by {
	ld	%1,#%2+1
	add	hl,%1
	; peephole 108 moved increment of hl to constant.
} if notUsed(%1)

replace restart {
	dec	hl
	ld	%1,#%2
	add	hl,%1
} by {
	ld	%1,#%2-1
	add	hl,%1
	; peephole 109 moved decrement of hl to constant.
} if notUsed(%1)

replace restart {
	inc	iy
	ld	%1, %2 (iy)
} by {
	ld	%1, %2+1 (iy)
	; peephole 110 moved increment of iy to offset.
} if notUsed('iy')

replace restart {
	push	hl
	pop	iy
	pop	hl
	inc	iy
} by {
	inc	hl
	push	hl
	pop	iy
	pop	hl
	; peephole 111 incremented in hl instead of iy.
}

replace restart {
	push	hl
	pop	iy
	inc	iy
} by {
	inc	hl
	push	hl
	pop	iy
	; peephole 111a incremented in hl instead of iy.
} if notUsed('hl')

replace restart {
	push	bc
	pop	iy
	inc	iy
} by {
	inc	bc
	push	bc
	pop	iy
	; peephole 111b incremented in bc instead of iy.
} if notUsed('bc')

replace restart {
	push	de
	pop	iy
	inc	iy
} by {
	inc	de
	push	de
	pop	iy
	; peephole 111c incremented in de instead of iy.
} if notUsed('de')

replace restart {
	ld	hl,%1
	add	hl,%2
	push	hl
	pop	iy
} by {
	ld	iy,%1
	add	iy,%2
	; peephole 111b added in iy instead of hl.
} if notUsed('hl'), notSame(%2 'hl')

replace restart {
	pop	af
	ld	sp,%1
} by {
	; peephole 112 removed redundant pop af.
	ld	sp,%1
} if notUsed('a')

replace restart {
	inc	sp
	ld	sp,%1
} by {
	; peephole 113 removed redundant inc sp.
	ld	sp,%1
} if notUsed('a')

replace restart {
	call    %1
	ret
} by {
	jp  %1
	; peephole 114 replaced call at end of function by jump (tail call optimization).
} if symmParmStack(%1)

// Callee saves ix.
replace restart {
	call	%1
	pop	ix
	ret
} by {
	pop	ix
	jp	%1
	; peephole115 replaced call at end of function by jump moving call beyond pop ix (tail call optimization).
} if symmParmStack(%1), notSame(%1 '____sdcc_cpu_pop_ei'), notSame(%1 '___sdcc_enter_ix')

replace restart {
	ld	%1,#%2
	ld	%3,%4
	ld	%1,#%2
} by {
	ld	%1,#%2
	ld	%3,%4
	; peephole 116 removed load of #%2 into %1 since it's still there.
} if notVolatile(%1), operandsNotRelated(%3 %1)

replace restart {
	ld	hl,#%1
	ld	de,#%1
} by {
	; peephole 117 used #%1 from hl for load into de.
	ld	hl,#%1
	ld	e,l
	ld	d,h
}

replace restart {
	ld	%1 (ix),l
	ld	%2 (ix),h
	ld	%3,%1 (ix)
	ld	%4,%2 (ix)
} by {	
	ld	%1 (ix),l
	ld	%2 (ix),h
	; peephole 118 used hl instead of %2 (ix), %1 (ix) to load %4%3.
	ld	%3,l
	ld	%4,h
} if operandsNotRelated('h' %3)
// Don't check for volatile since ix points to the stack.

replace restart {
	ld	%1, a
	ld	a, %2 (%3)
	adc	a, #%4
	ld	%6, %1
} by {
	ld	%6, a
	ld	a, %2 (%3)
	adc	a, #%4
	; peephole 119 loaded %6 from a directly instead of going through %1.
} if notUsed(%1), notSame(%6 'a')

replace restart {
	ld	%1, a
	ld	a, %2 (%3)
	adc	a, #%4
	ld	%5, a
	ld	%6, %1
} by {
	ld	%6, a
	ld	a, %2 (%3)
	adc	a, #%4
	ld	%5, a
	; peephole 120 loaded %6 from a directly instead of going through %1.
} if notUsed(%1), notSame(%5 %1), notSame(%5 '(hl)' '(de)' '(bc)'), notSame(%5 %6), notSame(%6 '(hl)' '(de)' '(bc)'), notSame(%5 'a'), notSame(%6 'a')

replace restart {
	ld	%1, a
	ld	a, #%2
	adc	a, #%3
	ld	%5, a
	ld	%6, %1
} by {
	ld	%6, a
	ld	a, #%2
	adc	a, #%3
	ld	%5, a
	; peephole 121 loaded %6 from a directly instead of going through %1.
} if notUsed(%1), notSame(%5 %1), notSame(%5 %6 '(hl)' '(de)' '(bc)'), notSame(%6 'a')

replace restart {
	ld	hl, #%1
	add	hl, %2
	ex	de, hl
	ld	hl, #%3
	add	hl, de
} by {
	ld	hl, #%1+%3
	add	hl, %2
	; peephole 122 removed addition and loads exploiting commutativity of addition.
} if notUsed('de')

replace restart {
	ld	%1,l
	ld	%2,h
	ex	de,hl
	ld	(hl),%1
	inc	hl
	ld	(hl),%2
} by {
	ld	%1,l
	ex	de,hl
	; peephole 122a used de instead of going through %1%2.
	ld	(hl),e
	inc	hl
	ld	(hl),d
} if notUsed(%2), notSame(%1 'l' 'h' 'e' 'd'), notSame(%2 'l' 'h' 'e' 'd')
	
replace restart {
	ld	e, l
	ld	d, h
	ld	hl, #0x0001
	add	hl, de
} by {
	ld	e, l
	ld	d, h
	inc	hl
	; peephole 123 replaced addition by increment.
}

replace restart {	
	ld      sp,hl
	ld      hl,#0x0002
	add     hl,sp
} by {
	ld	sp, hl
	inc	hl
	inc	hl
	; peephole 124 replaced addition by increment.
}

replace restart {
	ex	de, hl
	ld	hl, #%1
	add	hl, de
} by {
	; peephole 125 removed ex exploiting commutativity of addition.
	ld	de, #%1
	add	hl, de
} if notUsed('de')

replace restart {
	ex	de, hl
	push	bc
	ex	de, hl
} by {
	push	bc
	; peephole 126 canceled subsequent ex de, hl.
}

replace restart {
	ld	hl, #%1
	add	hl, %2
	ex	de, hl
	inc	de
} by {
	ld	hl, #%1+1
	; peephole 127 moved increment to constant.
	add	hl, %2
	ex	de, hl
} if notUsed('hl')

replace restart {
	ld	a,#0x01
	jp	%1
%2:
	xor	a,a
%1:
	sub	a,#0x01
	ld	a,#0x00
	rla
} by {
	xor	a,a
	jp	%1
%2:
	ld	a,#0x01
%1:
	; peephole 128 removed negation.
} if labelRefCount(%1 1)

replace restart {
	and	a,#0x01
	sub	a,#0x01
	ld	a,#0x00
	rla
} by {
	and	a,#0x01
	xor	a,#0x01
	; peephole 129 used xor for negation.
}

replace restart {
	or	a,a
	sub	a,#%1
} by {
	; peephole 130 removed redundant or.
	sub	a,#%1
}

replace restart {
	ld	a,#0x00
	rla
	sub	a,#0x01
	ld	a,#0x00
	rla
} by {
	ld	a,#0x00
	ccf
	; peephole 131 moved negation from bit 0 to carry flag.
	rla
}

replace restart {
	ld	a, #<(%1)
	add	a, l
	ld	l, a
	ld	a, #>(%1)
	adc	a, h
	ld	h, a
	push	bc
} by {
	push	bc
	ld	bc, #%1
	add	hl, bc
	; peephole 132 used 16 bit addition by moving push bc
	ld	a, h
} if notUsed('bc')

replace restart {
	pop	af
	push	hl
} by {
	; peephole 133 used ex to move hl onto the stack.
	ex	(sp),hl
} if notUsed('a'), notUsed('hl')

replace restart {
	pop	af
	ld	hl, #%1
	push	hl
} by {
	ld	hl, #%1
	; peephole 134 used ex to move hl onto the stack.
	ex	(sp),hl
} if notUsed('a'), notUsed('hl')

replace restart {
	pop	af
	inc	sp
	ld	hl,#%1
	push	hl
} by {
	inc	sp
	ld	hl,#%1
	; peephole 135 used ex to move #%1 onto the stack.
	ex	(sp),hl
} if notUsed('a'), notUsed('hl')

replace restart {
	pop	af
	ld	a,#%1
	push	af
	inc	sp
} by {
	ld	h,#%1
	ex	(sp),hl
	; peephole 136 used ex to move #%1 onto the stack.
	inc	sp
} if notUsed('a'), notUsed('hl')

replace restart {
	ld	%1,#%2
	ld	%3 (%1),a
%4:
	ld	%1,%5
} by {
	ld	(#%2 + %3),a
	; peephole 137 directly used #%2 instead of going through %1 using indirect addressing.
%4:
	ld	%1,%5
}

replace restart {
	pop	af
	ld	%1,#%2
	ld	%3 (%1),%4
	ld	%1,#%5
} by {
	ld	a,%4
	ld	(#%2 + %3),a
	; peephole 138 used #%2 directly instead of going through %1 using indirect addressing.
	pop	af
	ld	%1,#%5
} if notSame(%3 'a')

replace restart {
	ld	%1,a
	bit	%2,%1
} by {
	bit	%2,a
	; peephole 139 tested bit %2 of a directly instead of going through %1.
} if notUsed(%1)

replace restart {
	sbc	a,%1
	bit	7,a
	jp	Z,%2
} by {
	sbc	a,%1
	jp	P,%2
	; peephole 140 used sign flag instead of testing bit 7.
}

replace restart {
	sbc	a,%1
	bit	7,a
	jp	NZ,%2
} by {
	sbc	a,%1
	jp	M,%2
	; peephole 141 used sign flag instead of testing bit 7.
}

replace restart {
	dec %1
	ld	a, %1
	or	a, a
} by {
	dec %1
	; peephole 141a removed redundant transfer and flag setting in a
} if notUsed('a')

replace restart {
	ld	%1,a
	or	a,a
	jp	%3,%4
	ld	a,%1
} by {
	ld	%1,a
	or	a,a
	jp	%3,%4
	; peephole 142 used value still in a instead of reloading from %1.
}

replace restart {
	jp	%5
	ret
} by {
	jp	%5
	; peephole 143 removed unused ret.
}

replace restart {
	jp	%5
	ld	sp,ix
	pop	ix
	ret
} by {
	jp	%5
	; peephole 144 removed unused ret.
}

replace restart {
	or	a,%1
	jp	NZ,%2
	xor	a,a
	jp	%3
} by {
	or	a,%1
	jp	NZ,%2
	; peephole 145 removed redundant zeroing of a (which has just been tested to be #0x00).
	jp	%3
}


///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// SDCC PEEPHOLE RULES JP->JR ONLY FROM #9570
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

// peeph-z80.def - Z80 peephole rules
//
// Some of these peepholes could be potentially moved to peeph.def, but a
// GBZ80 expert should have a look at them before.
//
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2012
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

// These ex-generating rules should be among the last ones since ex counts as a read from both hl and de for notUsed().
barrier

replace restart {
	ld	d,h
	ld	e,l
} by {
	; peephole 146 used ex to load hl into de.
	ex	de,hl
} if notUsed('hl')

replace restart {
	ld	e,l
	ld	d,h
} by {
	; peephole 147 used ex to load hl into de.
	ex	de,hl
} if notUsed('hl')

replace restart {
	ld	l,e
	ld	h,d
} by {
	; peephole 148 used ex to load de into hl.
	ex	de,hl
} if notUsed('de')

barrier

// Should be one of the last ones. Opens the code to further peephole optimization.
replace restart {
%1:
} by {
	; peephole 149 removed unused label %1.
} if labelRefCount(%1 0)

// Ensure that all rules above see only jp, not jr.
barrier

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
%2:
} by {
	; peephole 150-3 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
%2:
} by {
	; peephole 150-3' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
%2:
} if labelJTInRange

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
%2:
} by {
	; peephole 150-4 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
%2:
} by {
	; peephole 150-4' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
%2:
} if labelJTInRange

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
%2:
} by {
	; peephole 150-5 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
%2:
} by {
	; peephole 150-5' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
%2:
} if labelJTInRange

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
	jp	%10
%2:
} by {
	; peephole 150-6 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
	jr	%10
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
	jp	%10
%2:
} by {
	; peephole 150-6' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
	jr	%10
%2:
} if labelJTInRange

replace {
	add	hl,de
	pop	de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
	jp	%10
	jp	%11
%2:
} by {
	; peephole 150-7 removed addition using short jumps in jump-table.
	pop	de
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
	jr	%10
	jr	%11
%2:
} if labelJTInRange

replace {
	add	hl,de
	jp	(hl)
%1:
	jp	%5
	jp	%6
	jp	%7
	jp	%8
	jp	%9
	jp	%10
	jp	%11
%2:
} by {
	; peephole 150-7' removed addition using short jumps in jump-table.
	jp	(hl)
%1:
	jr	%5
	jr	%6
	jr	%7
	jr	%8
	jr	%9
	jr	%10
	jr	%11
%2:
} if labelJTInRange

barrier

// Do all jump optimizations before replacing by ret.

replace restart {
	jp	%5
} by {
	ret
	; peephole 151 replaced jump by return.
} if labelIsReturnOnly(%5), labelRefCountChange(%5 -1)

replace restart {
	jp	%1,%5
} by {
	ret	%1
	; peephole 152 replaced jump by return.
} if labelIsReturnOnly(%5), labelRefCountChange(%5 -1)

// Replace jp by ret before replacing jp by jr outside of jump tables, since using ret can reduce the number of references to jump labels.
barrier

replace {
	jp	%5
} by {
	jr	%5
	; peephole 153 changed absolute to relative unconditional jump.
} if labelInRange(%5)

replace {
	jp	Z,%5
} by {
	jr	Z,%5
	; peephole 154 changed absolute to relative conditional jump.
} if labelInRange(%5)

replace {
	jp	NZ,%5
} by {
	jr	NZ,%5
	; peephole 155 changed absolute to relative conditional jump.
} if labelInRange(%5)

replace {
	jp	C,%5
} by {
	jr	C,%5
	; peephole 156 changed absolute to relative conditional jump.
} if labelInRange(%5)

replace {
	jp	NC,%5
} by {
	jr	NC,%5
	; peephole 157 changed absolute to relative conditional jump.
} if labelInRange(%5)

replace {
	dec b
	jr	NZ,	%5	
} by {
	djnz	%5
	; peephole 156 used djnz
}
